什么是对属性ConditionalAttribute吗? [英] What does ConditionalAttribute on an Attribute do?
问题描述
我知道什么是 ConditionalAttribute
一样。
该文档说,它也可以应用于类,如果
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的评论给了我一个想法。
@RicardoPontual's comment gave me an idea.
我这样做:
[Conditional("DEBUG")]
public class FooAttribute : Attribute { }
[Foo]
public class Bar { }
我的调试模式的编译,并且加载ILSpy(这是一个反汇编器)的DLL。这是我发现的,符合市场预期:
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 { }
然后我的发布模式的编制,并加载该DLL的ILSpy。这是我发现的:
Then I compiled in release mode, and loaded that DLL in ILSpy. This is what I found:
public class Bar { }
的
酒吧
类没有装饰这一次!
的因此,得到的答案是,当你装饰与条件
一些自定义的属性,那么该属性本身成为以同样的方式为条件。的
So, the answer is that when you decorate some custom attribute with Conditional
, then that attribute itself becomes conditional in the same way.
这就是我想要的行为。我最初以为从 ConditionalAttribute
来推导,但它的密封。相反,你需要来装饰你的自定义属性。
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.
这篇关于什么是对属性ConditionalAttribute吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!