¡C#!Condicional atributo?


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


Sé que C # tiene un Conditional atributo :

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

Que es equivalente1 a:

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

Pero en este caso quiero el comportamiento inverso (tienes que optar específicamente ):

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

Lo que me lleva a intentar:

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

Que no compila. Y:

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

Que no compila. Y:

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

Que no compila porque es solo una ilusión.

1No es cierto,pero es cierto. No me hagas traer de vuelta la esquina del Quisquilloso.

Author: LosManos, 2011-11-22

6 answers

Primero, tener el atributo Conditional es no equivalente a tener #if en su lugar el método. Considere:

ShowDebugString(MethodThatTakesAges());

Con el comportamiento real de ConditionalAttribute, MethodThatTakesAges no se llama-toda la llamada incluyendo el argumento evaluation se elimina del compilador.

Por supuesto, el otro punto es que depende de los símbolos del preprocesador en tiempo de compilación en el tiempo de compilación del llamador , no del método :)

Pero no, no creo que haya cualquier cosa que haga lo que quieras aquí. Acabo de comprobar la sección de especificaciones de C# 17.4.2 que trata con métodos condicionales y clases de atributos condicionales, y no hay nada allí que sugiera que hay tal mecanismo.

 49
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-13 07:49:58

No.

En su lugar, puede escribir

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

Tenga en cuenta que a diferencia de [Conditional], esto será determinado por la presencia del símbolo en su asamblea, no en la asamblea de su llamante.

 38
Author: SLaks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-22 17:00:37

Verdadero no podemos 'NO' Condicionalatribute, pero podemos 'NO' la condición como se presenta a continuación.

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

Espero que esto le ayude a resolver su problema;)

 15
Author: SoLaR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-13 00:09:22

Solo sumando mis 2 centavos, tres años más adelante :-) ... Utilizo un método [Conditional("DEBUG")] para establecer una propiedad IsDebugMode para comprobar lo contrario. Hacky, pero funciona:

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);
}
 8
Author: Heliac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-07 10:49:46
#ifndef ShowDebugString
#define RemoveSDS
#endif

?

Editar: Para mayor aclaración. Si se define ShowDebugString, se llamará a Conditional["ShowDebugString"]. Si ShowDebugString no está definido, se llamará a Conditional["RemoveSDS"].

 4
Author: Kyle W,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-22 16:38:17

La referencia anotada de la biblioteca estándar de NET framework no indica ninguna. Así que me temo que tendrá que rodar su propia!

 0
Author: Roy Dictus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-22 16:36:56