在类级别是否有条件属性? [英] Is there a Conditional attribute at the class level?

查看:72
本文介绍了在类级别是否有条件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个类的条件属性,或者更重要的是,有没有什么可以产生这种效果?基本上我不想让类在调试模式中存在。我也不想把每个电话都包在一个 #if DEBUG 指令中。

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.

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有一个发布在这种类型的事情今天,因为它发生。阅读它,看看它是否更有意义。

Eric Lippert had a post on just this sort of thing today, as it happens. 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.

这篇关于在类级别是否有条件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆