[英]In C# how can I check that XmlIgnore attribute is applied on a property or not?
[英]In C#, can an attribute be applied to a static class, method or property?
可以将属性应用于c#中的静态类,方法或属性吗? 喜欢:
[MyAttribute]
public static MyMethods(string str) ...
这里真的有两个问题
通常可以将属性应用于类,方法或属性吗?
是属性可以有效地针对任何这些结构(以及许多其他)
是否对特定属性有效?
这取决于具体的属性。 属性可以通过AttributeTargets
枚举控制它们可以应用于哪些构造,因此将特定属性应用于特定构造是非法的。
例如, ParamArrayAttribute
只能定位参数,而ObsoleteAttribute
几乎可以定位任何东西(除了程序集,可能还有一个我缺少的)
是的,您可以将属性应用于静态类,方法,属性。
例:
[MyAttribute("hello")]
public static string SayHello(string str)
{
return str;
}
是。 大概。 试着看看。
我想知道你为什么怀疑它。
是的,它可以应用。
如果它可以应用于静态类,它取决于属性。 查看AttributeTarget 。 因此,它是完全合法的,并允许使用Attributes修饰静态类或方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.