I know what ConditionalAttribute
does.
The docs say it can also be applied to a class, if it's derived from Attribute
:
[Conditional("DEBUG")]
public class FooAttribute : Attribute { }
But how does that custom attribute behave? (Is it stripped out of a release build?)
@RicardoPontual's comment gave me an idea.
I did this:
[Conditional("DEBUG")]
public class FooAttribute : Attribute { }
[Foo]
public class Bar { }
I compiled in debug mode, and loaded the DLL in ILSpy (it's a disassembler). This is what I found, as expected:
[Foo]
public class Bar { }
Then I compiled in release mode, and loaded that DLL in ILSpy. This is what I found:
public class Bar { }
The Bar
class was not decorated this time!
So, the answer is that when you decorate some custom attribute with Conditional
, then that attribute itself becomes conditional in the same way.
That's the behavior I wanted. I initially thought to derive from ConditionalAttribute
, but it's sealed. Instead you need to decorate your custom attribute.
来源:https://stackoverflow.com/questions/38982444/what-does-conditionalattribute-on-an-attribute-do