Ian*_*oyd 63 c# conditional conditional-compilation
C#有一个没有 Conditional(!Conditional,NotConditional,Conditional(!))属性?
我知道C#有一个Conditional属性:
[Conditional("ShowDebugString")]
public static void ShowDebugString(string s)
{
...
}
Run Code Online (Sandbox Code Playgroud)
这相当于1:
public static void ShowDebugString(string s)
{
#if ShowDebugString
...
#endif
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我想要反向行为(你必须明确选择退出):
public static void ShowDebugString(string s)
{
#if !RemoveSDS
...
#endif
}
Run Code Online (Sandbox Code Playgroud)
这让我尝试:
[!Conditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
Run Code Online (Sandbox Code Playgroud)
哪个不编译.和:
[Conditional("!RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
Run Code Online (Sandbox Code Playgroud)
哪个不编译.和:
[NotConditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
Run Code Online (Sandbox Code Playgroud)
这不会编译,因为它只是一厢情愿的想法.
Jon*_*eet 55
首先,具有该Conditional属性并不等同于具有#if该方法.考虑:
ShowDebugString(MethodThatTakesAges());
Run Code Online (Sandbox Code Playgroud)
有了真实的行为ConditionalAttribute,MethodThatTakesAges就不会被调用 - 从编译器中删除包括参数评估在内的整个调用.
当然另一点是它依赖于调用者编译时的编译时预处理器符号,而不是方法 :)
但不,我不相信有什么能满足你的需求.我刚刚检查了C#规范部分17.4.2,该部分处理条件方法和条件属性类,并且没有任何内容表明存在任何此类机制.
SLa*_*aks 43
不.
相反,你可以写
#if !ShowDebugString
[Conditional("FALSE")]
#endif
Run Code Online (Sandbox Code Playgroud)
请注意,与[Conditional]此不同,这将取决于程序集中符号的存在,而不是调用者程序集中的符号.
SoL*_*LaR 18
确实,我们不能'不'ConditionalAttribute,但我们可以'不'条件如下所示.
// at the begining of the code before uses
#if DUMMY
#undef NOT_DUMMY
#else
#define NOT_DUMMY
#endif
// somewhere in class
[Conditional("NOT_DUMMY")]
public static void ShowDebugStringNOTDUMMY(string s)
{
Debug.Print("ShowDebugStringNOTDUMMY");
}
[Conditional("DUMMY")]
public static void ShowDebugStringDUMMY(string s)
{
Debug.Print("ShowDebugStringDUMMY");
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助您解决问题;)
Hel*_*iac 13
只需添加我的2美分,三年下线:-) ...我使用一种[Conditional("DEBUG")]方法设置IsDebugMode属性来检查反向.Hacky,但它有效:
private bool _isDebugMode = false;
public bool IsDebugMode
{
get
{
CheckDebugMode();
return _isDebugMode;
}
}
[Conditional("DEBUG")]
private void CheckDebugMode()
{
_isDebugMode = true;
}
private void DisplaySplashScreen()
{
if (IsDebugMode) return;
var splashScreenViewModel = new SplashScreenVM(500)
{
Header = "MyCompany Deals",
Title = "Main Menu Test",
LoadingMessage = "Creating Repositories...",
VersionString = string.Format("v{0}.{1}.{2}",
GlobalInfo.Version_Major, GlobalInfo.Version_Minor, GlobalInfo.Version_Build)
};
SplashScreenFactory.CreateSplashScreen(splashScreenViewModel);
}
Run Code Online (Sandbox Code Playgroud)
#ifndef ShowDebugString
#define RemoveSDS
#endif
Run Code Online (Sandbox Code Playgroud)
?
编辑:更多说明。如果定义了 ShowDebugStringConditional["ShowDebugString"]将被调用。如果 ShowDebugString 未定义,Conditional["RemoveSDS"]将被调用。