.net - C# Preprocessor Directives in the IL code -
i try use simple preprocessor directives debug , release mode in c#.
for example:
using system; public class c { public void m() { #if debug console.writeline("debug"); #else console.writeline("release"); #endif } } this code simple , clear.
but when see il code don't have debug directive.
.class private auto ansi '<module>' { } // end of class <module> .class public auto ansi beforefieldinit c extends [mscorlib]system.object { // methods .method public hidebysig instance void m () cil managed { // method begins @ rva 0x2050 // code size 13 (0xd) .maxstack 8 il_0000: nop il_0001: ldstr "release" il_0006: call void [mscorlib]system.console::writeline(string) il_000b: nop il_000c: ret } // end of method c::m .method public hidebysig specialname rtspecialname instance void .ctor () cil managed { // method begins @ rva 0x205e // code size 8 (0x8) .maxstack 8 il_0000: ldarg.0 il_0001: call instance void [mscorlib]system.object::.ctor() il_0006: nop il_0007: ret } // end of method c::.ctor } // end of class c it's interesting me, why happens.
i mean why don't see debug mode in il code?
p.s. use sharplab see il code.
p.s.2 live example
it because preprocessor directives used compiler generate different il code based on configuration. not used clr.
as why sharplab isn't doing right thing, uses rosyln , sets optimization settings. doesn't set preprocessor variable.
public void setoptimize([notnull] iworksession session, [notnull] string optimize) { var project = session.roslyn.project; var options = ((csharpcompilationoptions)project.compilationoptions); session.roslyn.project = project.withcompilationoptions( options.withoptimizationlevel(optimize == optimize.debug ? optimizationlevel.debug : optimizationlevel.release) ); } i suggest open issue sharplab implement work expected.
Comments
Post a Comment