C# !Conditional attribute?

徘徊边缘 提交于 2019-11-27 01:26:22

问题


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


i know C# has a Conditional attribute:

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

which is equivalent1 to:

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
}

Which leads me to try:

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

which doesn't compile. And:

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

which doesn't compile. And:

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

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

1 Not true, but true enough. Don't make me bring back the Nitpicker's Corner.


回答1:


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

ShowDebugString(MethodThatTakesAges());

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

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 :)

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




回答2:


Nope.

Instead, you can write

#if !ShowDebugString
[Conditional("FALSE")]
#endif

Note that unlike [Conditional], this will be determined by the presence of the symbol in your assembly, not in your caller's assembly.




回答3:


True we can't 'NOT' ConditionalAttribute, but we can 'NOT' the condition as presented below.

// 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");
}

hope this helps you solve your problem ;)




回答4:


Just adding my 2 cents, three years down the line :-) ... I use a [Conditional("DEBUG")] method to set an IsDebugMode property to check the reverse. Hacky, but it works:

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);
}



回答5:


#ifndef ShowDebugString
#define RemoveSDS
#endif

?

edit: For more clarification. If ShowDebugString is defined Conditional["ShowDebugString"] will be called. If ShowDebugString is not defined, Conditional["RemoveSDS"] will be called.




回答6:


The NET framework standard library annotated reference doesn't state any. So I'm afraid you'll have to roll your own!



来源:https://stackoverflow.com/questions/8230191/c-sharp-conditional-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!