What does ConditionalAttribute on an Attribute do?

六月ゝ 毕业季﹏ 提交于 2019-12-06 10:39:20

@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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!