什么是对属性ConditionalAttribute吗?

编程入门 行业动态 更新时间:2024-10-24 04:43:23
本文介绍了什么是对属性ConditionalAttribute吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道什么是 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吗?

本文发布于:2023-11-06 01:26:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   ConditionalAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!