C#!Conditional属性? [英] C# !Conditional attribute?

查看:263
本文介绍了C#!Conditional属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#有一个的条件!条件 NotConditional 条件(!))的属性?

Does C# have a not Conditional (!Conditional, NotConditional, Conditional(!)) attribute?

我知道C#有一个条件属性:

i know C# has a Conditional attribute:

[Conditional("ShowDebugString")]
public static void ShowDebugString(string s)
{
   ...
}

这相当于 1

public static void ShowDebugString(string s)
{
#if ShowDebugString
   ...
#endif
}

但在这种情况下,我想的的行为(你有专门的退出的):

But in this case i want the inverse behavior (you have to specifically opt out):

public static void ShowDebugString(string s)
{
#if !RemoveSDS
   ...
#endif
}

这使我尝试:

[!Conditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
   ...
}

不编译。和:

[Conditional("!RemoveSDS")]
public static void ShowDebugString(string s)
{
   ...
}

不编译。和:

[NotConditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
   ...
}

不编译,因为这只是一厢情愿的想法。

which doesn't compile because it's only wishful thinking.

1 不是真的,但足够真实。不要让我带回了都舍不得亏的园地。

推荐答案

首先,具有条件属性的的等价于有#如果而不是方法。试想一下:

First, having the Conditional attribute is not equivalent to having #if instead the method. Consider:

ShowDebugString(MethodThatTakesAges());

随着 ConditionalAttribute MethodThatTakesAges 不会被调用真正的行为 - 在整个呼叫的包括参数评测的从编译器中删除。

With the real behaviour of ConditionalAttribute, MethodThatTakesAges doesn't get called - the entire call including argument evaluation is removed from the compiler.

当然的另一点是,它依赖于编译时preprocessor符号在主叫的的编译时间,而不是的方法的:)

Of course the other point is that it depends on the compile-time preprocessor symbols at the compile time of the caller, not of the method :)

但是,没有,我不相信有什么事该做你想要的这里。我刚刚检查了C#规范第17.4.2它与条件方法和条件属性类的交易,并没有什么在那里暗示有任何这样的机制。

But no, I don't believe there's anything which does what you want here. I've just checked the C# spec section 17.4.2 which deals with conditional methods and conditional attribute classes, and there's nothing in there suggesting there's any such mechanism.

这篇关于C#!Conditional属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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