从事件奇怪的反编译code认购code在泛型类 [英] Strange decompiled code from event subscribe code in a generic class

查看:454
本文介绍了从事件奇怪的反编译code认购code在泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的类

 公共类测试< T>
{
    公共静态无效A(WA窗口,窗口WB)
    {
        wa.Closed + =(S,E)=> wb.Close();
    }
}
 

被编译到这个(我使用反射来反编译):

 公共类测试< T>
{
    [编译器生成]
    私人密封类<> c__DisplayClass1
    {
        公共窗口WB;

        公共无效< A> b__0(对象S,EventArgs的五)
        {
            this.wb.Close();
        }
    }

    公共静态无效A(WA窗口,窗口WB)
    {
        wa.Closed + =委托(对象S,EventArgs的五)
        {
            base.wb.Close();
        };
    }
}
 

什么是基础的含义?为什么<> c__DisplayClass1 生成的,如果是从来没用过? 这是一个反射器的bug?

编辑: 的确,好像反射优化不是沃金非常好,在此情况下,禁用优化的反编译code有意义的:

 公共类测试< T>
{
    公开测试()
    {
        base..ctor();
        返回;
    }

    公共静态无效A(WA窗口,窗口WB)
    {
        <> c__DisplayClass1< T> CS $<> 8__locals2;
        CS $<> 8__locals2 =全新<> c__DisplayClass1< T>();
        CS $<> 8__locals2.wb = WB;
        wa.Closed + =新的EventHandler(CS $<> 8__locals2< A> b__0);
        返回;
    }

    [编译器生成]
    私人密封类<> c__DisplayClass1
    {
        //领域
        公共窗口WB;

        公众<> c__DisplayClass1()
        {
            base..ctor();
            返回;
        }

        公共无效< A> b__0(对象S,EventArgs的五)
        {
            this.wb.Close();
            返回;
        }
    }
}
 

解决方案

反射器优化的输出,试图搞出什么C#的可能模样。我不知道那里的基地位从,无可否认未来......但是生成的类的的绝对被使用。

设置反射选项为未优化,你会看到更多的东西像一个IL C#的直接转换。或者只是切换到IL和直接读取它,如果你想有一个pretty的原始观点。

This simple class

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

Gets compiled to this (I'm using 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's never used ? Is this a Reflector bug ?

Edit: Indeed, seems like Reflector optimisation isn't woking very well in this case, disabling the optimisation the decompiled code 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;
        }
    }
}

解决方案

Reflector is "optimising" the output to try to come up with what the C# might have looked like. I don't know where the "base" bit is coming from, admittedly... but the generated class is definitely being used.

Set the Reflector options to "unoptimised" and you'll see something more like an IL to C# direct conversion. Or just switch to IL and read it directly, if you want a pretty raw view.

这篇关于从事件奇怪的反编译code认购code在泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆