Conditional Attribute vs. #if


sample code.

all want attribute tells compiler, "skip this".

i hate using #if , #endif.  easy loose them. 

(it nice if outlined themselves, gimme switch top , bottom please, not @ top.)

 

code block

 

#define compile

#define donotcompile

#undef donotcompilethismethod

 

using system;

using system.collections.generic;

using system.text;

using system.diagnostics;

 

namespace csharp

{

class conditionalcompile

{

[conditional("compile")]

void methoda() { }

 

#if !compile

void methodb() { }

#endif

 

#if donotcompile

void methodc() { }

#endif

 

#if donotcompilethismethod

void methodd() { }

#endif

 

[conditional("condition1")]

public static void method1() { }

[conditional("condition1"), conditional("condition2")]

public static void method2() { }

[conditional("donotcompile")]

void method3() { }

[conditional("donotcompilethismethod")]

void method4() { }

}

}

 

 

 

 

this results in 6 methods being compiled:

methoda

methodc

method1, method2, method3, , method4.

 

can explain why methods compiled, if pre-processor directives not defined?  documentation these old eyes see appear suggest method1 , method2 should not created.

 

thanx ahead of time.

the methods compiled, calls not. conditionalattribute class affects whether method callable, , can ignored compiler. also, works if there no return type on method.

 

here's link msdn library , quote explains this:

 

when conditionalattribute applied method, indicates compilers call method should not compiled microsoft intermediate language (msil) unless conditional compilation symbol associated conditionalattribute defined. when conditionalattribute applied attribute, indicates attribute should not emitted metadata unless conditional compilation symbol defined. arguments passed method or attribute still type checked compiler.



Archived Forums V  >  Visual C# IDE



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'