Strange decompiled code from event subscription code in a common class

This simple class

public class Test<T> { public static void A(Window wa, Window wb) { wa.Closed += (s, e) => wb.Close(); } } 

Gets the compiled data (I use Reflector to decompile):

 public class Test<T> { [CompilerGenerated] private sealed class <>c__DisplayClass1 { public Window wb; public void <A>b__0(object s, EventArgs e) { this.wb.Close(); } } public static void A(Window wa, Window wb) { wa.Closed += delegate(object s, EventArgs e) { base.wb.Close(); }; } } 

What is the meaning of base ? Why is <>c__DisplayClass1 generated if it has never been used? Is this a reflector error?

Edit: Indeed, it seems that reflector optimization is not very good in this case, disabling decompiled code optimization makes sense:

 public class Test<T> { public Test() { base..ctor(); return; } public static void A(Window wa, Window wb) { <>c__DisplayClass1<T> CS$<>8__locals2; CS$<>8__locals2 = new <>c__DisplayClass1<T>(); CS$<>8__locals2.wb = wb; wa.Closed += new EventHandler(CS$<>8__locals2.<A>b__0); return; } [CompilerGenerated] private sealed class <>c__DisplayClass1 { // Fields public Window wb; public <>c__DisplayClass1() { base..ctor(); return; } public void <A>b__0(object s, EventArgs e) { this.wb.Close(); return; } } } 
+4
source share
1 answer

Reflector β€œoptimizes” the output to try and find what C # looked like. I don’t know where the "base" bit comes from, really ... but the generated class is definitely used.

Set the Reflector parameters to "unoptimised" and you will see something more than a direct IL-C # conversion. Or just switch to IL and read it directly if you want a pretty crude presentation.

+1
source

Source: https://habr.com/ru/post/1314445/


All Articles