[英]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.