il

When I use is operator why there is only a null-check in IL code?

纵然是瞬间 提交于 2019-12-06 18:31:36
问题 I was wondering how is is operator implemented in C# .And I have written a simple test program (nothing special, just for demonstration purposes): class Base { public void Display() { Console.WriteLine("Base"); } } class Derived : Base { } class Program { static void Main(string[] args) { var d = new Derived(); if (d is Base) { var b = (Base) d; d.Display(); } } } And looked at the generated IL code: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code

Is it possible to inspect an assembly's IL instructions programmatically using managed code?

橙三吉。 提交于 2019-12-06 15:57:47
See title. Reflection.Emit seems to be more about creating a new dynamic assembly, not for loading an exisitng assembly and inspecting its IL. Common Compiler Infrastructure Reflector does this, and last time I checked, Reflector could still inspect (i.e. disassemble) itself this way, so it will show you exactly how it works. 来源: https://stackoverflow.com/questions/2824086/is-it-possible-to-inspect-an-assemblys-il-instructions-programmatically-using-m

Does initialization of local variable with null impacts performance?

天大地大妈咪最大 提交于 2019-12-06 00:39:13
问题 Lets compare two pieces of code: String str = null; //Possibly do something... str = "Test"; Console.WriteLine(str); and String str; //Possibly do something... str = "Test"; Console.WriteLine(str); I was always thinking that these pieces of code are equal. But after I have build these code (Release mode with optimization checked) and compared IL methods generated I have noticed that there are two more IL instructions in the first sample: 1st sample code IL: .maxstack 1 .locals init ([0]

Difference between call instance vs newobj instance in IL

本秂侑毒 提交于 2019-12-05 21:49:47
问题 I'm delving into C# in Depth, and playing with nullable value types. Just for experimental purposes I wrote a piece of code: private static void HowNullableWorks() { int test = 3; int? implicitConversion = test; Nullable<int> test2 = new Nullable<int>(3); MethodThatTakesNullableInt(null); MethodThatTakesNullableInt(39); } And I was supprised to see that implicitConversion / test2 variables are initialized with: call instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)

Is there a generic CIL code to convert any type instance to string?

北战南征 提交于 2019-12-05 10:56:39
Is it possible to write generic CIL instructions which will convert instances of any type (both value and reference) to System.String? In particular, I'm interested in Mono.Cecil code which will inject those instructions into a method. Analyzing a generic method I came up with these Mono.Cecil calls: (it's supposed to convert the i-th method parameter to string) System.Reflection.MethodInfo to_string_method_info = typeof( System.Object ).GetMethod( "ToString" ); Mono.Cecil.MethodReference to_string_reference = injectible_assembly.MainModule.Import( to_string_method_info ); Mono.Cecil

Questions about hand coded IL based on disassembled simple C# code

廉价感情. 提交于 2019-12-05 07:03:07
I just started looking at IL a bit and I'm curious if my attempt (shown below) to remove excess code from the output of the compiler had any unintended side effects. A couple of quesiton about the results: What is the purpose of the nop operations in the original? What is the purpose of the br.s at the end of the methods in the original? Is the re-written version improper in any way? Original C# Code: class Program { public static int Main() { return Add(1, 2); } public static int Add(int a, int b) { return a + b; } } Compiled with csc.exe and disassembled it with ildasm.exe (Original):

call instead of callvirt in case of the new c# 6 “?” null check

久未见 提交于 2019-12-05 00:35:12
Given the two methods: static void M1(Person p) { if (p != null) { var p1 = p.Name; } } static void M2(Person p) { var p1 = p?.Name; } Why the M1 IL code use callvirt : IL_0007: brfalse.s IL_0012 IL_0009: nop IL_000a: ldarg.0 IL_000b: callvirt instance string ConsoleApplication4.Person::get_Name() and the M2 IL use call : brtrue.s IL_0007 IL_0004: ldnull IL_0005: br.s IL_000d IL_0007: ldarg.0 IL_0008: call instance string ConsoleApplication4.Person::get_Name() I just can guess that it because in M2 we know that p isn't null and its like new MyClass().MyMethod(); Is it true? If it is, what if p

When I use is operator why there is only a null-check in IL code?

时光怂恿深爱的人放手 提交于 2019-12-05 00:02:38
I was wondering how is is operator implemented in C# .And I have written a simple test program (nothing special, just for demonstration purposes): class Base { public void Display() { Console.WriteLine("Base"); } } class Derived : Base { } class Program { static void Main(string[] args) { var d = new Derived(); if (d is Base) { var b = (Base) d; d.Display(); } } } And looked at the generated IL code: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 27 (0x1b) .maxstack 2 .locals init ([0] class ConsoleApplication1.Derived d, [1] bool V_1, [2]

Why does LambdaExpression.Compile() work on iOS (Xamarin)?

南笙酒味 提交于 2019-12-04 17:07:04
问题 Since Xamarin.iOS doesn't support code generation at runtime, why do Compile() and DynamicInvoke() work as expected? For example, the following code works fine: var lambda = Expression.Lambda( Expression.Add( Expression.Constant(1), Expression.Constant(2) ) ); var f = lambda.Compile(); var result = f.DynamicInvoke(); // result==3 at this point Is Xamarin evaluating the expression tree at runtime instead of emitting IL code? 回答1: On platforms that support code generation, Reflection.Emit-based

Is IL generated by expression trees optimized?

帅比萌擦擦* 提交于 2019-12-04 11:41:24
问题 Ok this is merely curiosity, serves no real world help. I know that with expression trees you can generate MSIL on the fly just like the regular C# compiler does. Since compiler can decide optimizations, I'm tempted to ask what is the case with IL generated during Expression.Compile() . Basically two questions: Since at compile time the compiler can produce different (may be slightly) IL in debug mode and release mode , is there ever a difference in the IL generated by compiling an expression