C # using Conditional () with environment variable

How to use the conditional ("Condition1") attribute, where "Condition1" is an environment variable. Help says this can be done, but does not say how?

+6
c # visual-studio-2010
source share
3 answers

I believe this is what you are looking for http://yogesh.jagotagroup.com/blog/post/2008/03/01/CSharp-Conditional-attribute.aspx

The word "environment variable" refers to the VS environment, and not to the OS or any other. You can simply tag your classes or methods with DEBUG or any custom variable; then these classes and methods still exist at compilation, but when the IL-code is generated, they are simply skipped - in case the variable is not defined (for example, the example says).

Hope this helps.

Pz, developer of TaskConnect

+5
source share

Not sure what Conditional(string condition) does, but you can get environment variables using Environment.ExpandEnvironmentVariables .


If you are talking about a conditional attribute, you're out of luck. This attribute is evaluated at COMPILE TIME, on your development machine (or on the build server). This does not affect RUNTIME, where you would like to do something different depending on the state of the user machine (as indicated by the environment variable).

You can use the previously mentioned method of the Environment class to determine whether the execution should continue inside the method body. This is probably your best bet.

+3
source share

You do not need to do anything except create an envionment variable (for example, with a value of 1), and then refer to it by name in the conditional attribute. This variable must be created in the "Environment Variables" dialog box, accessible through the "Advanced" tab of the "System Properties" dialog box; that is, you cannot just open the command line and set a variable there, since this value will exist only in this invitation and will be lost when you close the invitation.

0
source share

All Articles