简体   繁体   English

为什么我不能在类上使用ConditionalAttribute?

[英]Why can't I use ConditionalAttribute on a class?

I look into ConditionalAttribute declaration and it is declared like this: 我查看ConditionalAttribute声明,它声明如下:

I found JavaScript code that goes like this: 我发现JavaScript代码是这样的:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,
   AllowMultiple = true)]
public sealed class ConditionalAttribute : Attribute {
  //whatever
}

and AttributeTargets.Class is claimed to mean that Attribute can be applied to a class. 声称AttributeTargets.Class表示Attribute可以应用于类。 so I tried this: 所以我试过这个:

[Conditional("DEBUG")]
class MyClass
{
}

but the compiler emits the following error 但编译器发出以下错误

error CS1689: Attribute 'System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes 错误CS1689:属性'System.Diagnostics.ConditionalAttribute'仅对方法或属性类有效

and MSDN says 和MSDN说

This error only occurs with the ConditionalAttribute attribute. 只有ConditionalAttribute属性才会出现此错误。 As the message states, this attribute can only be used on methods or attribute classes. 如消息所述,此属性只能用于方法或属性类。 For example, trying to apply this attribute to a class will generate this error. 例如,尝试将此属性应用于类将生成此错误。

So it looks like there's an attribute declared to be applicable to a class but trying to apply it to a class causes a compilation error. 所以看起来有一个属性声明适用于类但是尝试将它应用于类会导致编译错误。

How is this possible? 这怎么可能? Is that some hardwired special case or what? 这是一些硬连线的特殊情况还是什么?

Okay: 好的:

[Conditional("DEBUG")]
public void foo() { }

Okay too: 还好吧:

[Conditional("DEBUG")]
public class BarAttribute : Attribute { }

Not okay: 不行:

[Conditional("DEBUG")]
public class Baz { }

ConditionalAttribute can be applied to classes, but there's an additional restriction of the class being an attribute class . ConditionalAttribute可以应用于类,但是作为属性类的类还有一个额外的限制

If you want the whole class to be removed based on conditional define, then no, you can't to it. 如果你想根据条件定义删除整个类,那么不,你不能这样做。 It's not supported. 它不受支持。 You'll have to mark each method individually. 您必须单独标记每种方法。

Yes, ConditionalAttribute is a special case, being one of only a few attributes that are specifically handled directly by the compiler. 是的, ConditionalAttribute是一种特殊情况,是由编译器直接专门处理的少数几个属性之一。

The compiler would have no well-defined behaviour in that case, so it chooses not to let you do it, to avoid confusion. 在这种情况下,编译器没有明确定义的行为,所以它选择不让你这样做,以避免混淆。

Of course, technically you could write a non-attribute class in MSIL that is marked with ConditionalAttribute , compile that with ilasm , and then reference it from a C# project - it would be interesting to know what the C# compiler does... I'm guessing it would do nothing special unless individual methods had the method too, since that is the scenario it targets. 当然,从技术上讲,你可以在MSIL中编写一个用ConditionalAttribute标记的非属性类,用ilasm编译它,然后从C#项目中引用它 - 知道C#编译器做什么会很有趣......我我它没有什么特别的,除非个别方法也有这个方法,因为那是它所针对的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM