简体   繁体   English

类级别是否有Conditional属性?

[英]Is there a Conditional attribute at the class level?

I want to use the conditional attribute on a class, or more to the point, is there something that give that effect? 我想在类上使用条件属性,或者更重要的是,是否存在可以产生这种效果的东西? Basically I don't want the class to be there in debug mode. 基本上我不希望类在调试模式下存在。 I also don't want to have to wrap each call in a #if DEBUG directive. 我也不想在#if DEBUG指令中包装每个调用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace NameSpace
{
    [Conditional("Debug")]
    public class ClassName
    {

        public ClassName()
        {
        }
    }
}

No, there isn't. 不,没有。 Conditional attributes don't make their targets disappear themselves - they just make the compiler omit users of the targets. 条件属性不会使它们的目标自行消失 - 它们只会使编译器省略目标的用户

Eric Lippert had a post on just this sort of thing today, as it happens. Eric Lippert今天发表了一篇关于此类事情的文章。 Read it and see if it all makes more sense to you. 阅读它,看看它是否对你更有意义。

If you really need to omit the class itself in release mode, then use preprocessor directives - but you'll have to do the same for all the callers as well. 如果你真的需要在发布模式下省略类本身,那么使用预处理器指令 - 但是你也必须为所有调用者做同样的事情。 What harm does it have to keep the class around in release mode, anyway? 无论如何,它有什么危害才能让班级处于发布模式?

Could this actually be a class in a different project? 这实际上可能是一个不同项目中的一个类吗? If so, then you could just apply the conditional attribute to all the methods, then the type wouldn't be needed in release mode, so you could avoid shipping the assembly. 如果是这样, 那么您可以将条件属性应用于所有方法,然后在发布模式下不需要该类型,因此您可以避免运送程序集。

Not sure, if I have right understanding of this. 不确定,如果我对此有正确的理解。

But, if you decorate all the methods with ConditionalAttribute - it all will be removed when the symbol is not present. 但是,如果使用ConditionalAttribute修饰所有方法 - 当符号不存在时,将删除所有方法。 And hence, although the class is available for use, there won't be any methods at runtime to use it for. 因此,尽管该类可以使用,但在运行时不会有任何方法可以使用它。

I am not sure, why you would want to exclude a class depending upon the symbol? 我不确定,为什么你想根据符号排除一个类? Could you explain the scenario? 你能解释一下情景吗?

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

相关问题 取决于类属性的条件方法 - Conditional method depending on class attribute 类级别的可本地化的DisplayName属性 - Localizable DisplayName attribute for class level 实现一个类级别的验证属性,该属性将写入类中的属性 - Implementing a class-level validation attribute that writes to a property inside the class 条件属性 - Conditional Attribute 如何将条件必需属性放入类属性中以使用 WEB API? - How to put conditional Required Attribute into class property to work with WEB API? 如何从程序集级属性引用私有类的类型? - How to reference the type of a private class from an assembly-level attribute? .net中的类级别的RefreshProperties属性,winforms +错误地刷新了属性网格 - RefreshProperties attribute at class level in .net, winforms + incorrectly refreshing property grid 私有const在类级别定义,然后在属性上使用,为什么要编译? - Private const defined on class level and then used on an attribute, why this compile? 如何在类级别从PropertyInfo获取DataContract属性? - How do I get DataContract attribute from PropertyInfo at the class level? C#在属性类上使用[System.Diagnostics.Conditional]属性实际上会做什么 - C# What does using the [System.Diagnostics.Conditional] attribute on an attribute class actually do
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM