Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-conditionalmethods (Conditional method) |
1.A, example (sample) back to top |
Conditional Methods Sample
This example demonstrates conditional methods, which provide a powerful mechanism through which method calls can be included or omitted depending on whether a symbol is defined.
Safety Instructions |
This code example is provided to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes. |
To build and run the conditional methods sample in Visual Studio
In Solution Explorer, right-click the project and then select Properties.
Open the Configuration Properties folder and click Debug.
Set the command line Arguments property to "A B C" (without the quotation marks).
In the Configuration Properties folder, click Build.
Modify the conditional compilation constants property (for example, add or remove DEBUG), and then click OK.
On the Debug menu, click Start Without Debugging.
To build and run the conditional methods sample from the command line
1.B, sample code back to top |
1.b.1, CondMethod.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //CondMethod.cs//compile-time use:/target:library/d:debugusingSystem;usingSystem.Diagnostics;namespacetracefunctions { Public classTrace {[Conditional ("DEBUG")] Public Static voidMessage (stringtracemessage) {Console.WriteLine ("[TRACE]-"+tracemessage); } } }
View Code
1.b.2, Tracetest.cs [Main]
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //TraceTest.cs//compile-time use:/reference:condmethod.dll//parameter: A B CusingSystem;usingtracefunctions; Public classtraceclient { Public Static voidMain (string[] args) {Trace.message ("Main Starting"); if(args. Length = =0) {Console.WriteLine ("No arguments have been passed"); } Else { for(intI=0; I < args. Length; i++) {Console.WriteLine ("arg[{0}] is [{1}]", I,args[i]); }} trace.message ("Main Ending"); } }
View Code
1.b.3,
1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-languagesamples-conditionalmethods (Conditional method)