Use conditional metadata attribute in C # To implement debug code

Source: Internet
Author: User

1 [Conditional ( " Debug " )]
2 Private   Void Debug_trace ( String Strmsg)
3 {
4Console. writeline (strmsg );
5}

this statement is clear. The Code is easy to read and can shield potential bugs. 1 debug_trace ( " error message1 " );
2 console. writeline ( " message " );
3 debug_trace ( " error message2 " );

For this codeReleaseGenerated inIlThe Code is the same as the following code.Console. writeline ("Message");

HoweverConditionalThe attribute function has a limit, that is, it cannot return values, that is, it must useVoidTo define the return value.

AndConditionalTo add multiple attributes, for example:

[Conditional ("debug"), conditional ("trace")]

Private VoidDebug_trace (StringStrmsg)

{

Console. writeline (strmsg );

}

 

However, the relationship between these two attributes is or, that is,"Debug"Or"Trace"If any one is defined, this code will be executed. If you need to add two parallel attributes, directly useConditionalIt cannot be implemented.If/endifAs follows:

# If(Debug & trace)

# DefineBoth

# Endif

[Conditional ("both")]

Private VoidDebug_trace (StringStrmsg)

{

Console. writeline (strmsg );

}

 

The method has already been mentioned here. It can be basically done. Next, let's talk about how to set environment variables inC #There are three setting methods.

Method 1:ProgramAnd must be defined at the beginning of a file;

Method 2: set it in the compilation command line of the project;

Method 3: Add environment variables to the operating system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.