il

Viewing the IL code generated from a compiled expression

北城以北 提交于 2019-11-26 12:07:07
问题 Is it possible to view the IL code generated when you call Compile() on an Expression tree? Consider this very simple example: class Program { public int Value { get; set; } static void Main(string[] args) { var param = Expression.Parameter(typeof(Program)); var con = Expression.Constant(5); var prop = Expression.Property(param, typeof(Program).GetProperty(\"Value\")); var assign = Expression.Assign(prop, con); Action<Program> lambda = Expression.Lambda<Action<Program>>(assign, param).Compile

Why is the C# compiler emitting a callvirt instruction for a GetType() method call?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:06:39
问题 I am curious to know why this is happening. Please read the code example below and the corresponding IL that was emitted in comments below each section: using System; class Program { static void Main() { Object o = new Object(); o.GetType(); // L_0001: newobj instance void [mscorlib]System.Object::.ctor() // L_0006: stloc.0 // L_0007: ldloc.0 // L_0008: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType() new Object().GetType(); // L_000e: newobj instance void

Performance of static methods vs instance methods

烈酒焚心 提交于 2019-11-26 07:17:10
问题 My question is relating to the performance characteristics of static methods vs instance methods and their scalability. Assume for this scenario that all class definitions are in a single assembly and that multiple discrete pointer types are required. Consider: public sealed class InstanceClass { public int DoOperation1(string input) { // Some operation. } public int DoOperation2(string input) { // Some operation. } // … more instance methods. } public static class StaticClass { public static