簡體   English   中英

具有新實例的C#反射發射函數

[英]C# Reflection Emit Func with new instance

我正在嘗試通過反射發出以下代碼:

 this.Inputs = new DynamicCollection<IFunctionInput<Digital>>((string name) => new FunctionInput<Digital>(this, name));

DynamicCollection的ctor帶有一個func,如下所示:

public DynamicCollection ( Func<string, T> createPin )

但是,當我查看編譯的IL代碼時,它給了我:

IL_0008: ldarg.0
    IL_0009: ldarg.0
    IL_000a: ldftn instance class [CM.Shared]CM.Shared.IFunctionInput`1<valuetype [CM.Shared]CM.Shared.Digital> CM.Server.LogicalAndFunctionServer::'<.ctor>b__0_0'(string)
    IL_0010: newobj instance void class [mscorlib]System.Func`2<string, class [CM.Shared]CM.Shared.IFunctionInput`1<valuetype [CM.Shared]CM.Shared.Digital>>::.ctor(object,  native int)
    IL_0015: newobj instance void class CM.Server.DynamicCollection`1<class [CM.Shared]CM.Shared.IFunctionInput`1<valuetype [CM.Shared]CM.Shared.Digital>>::.ctor(class [mscorlib]System.Func`2<string, !0>)
    IL_001a: stfld class [CM.Shared]CM.Shared.IDynamicCollection`1<class [CM.Shared]CM.Shared.IFunctionInput`1<valuetype [CM.Shared]CM.Shared.Digital>> CM.Server.LogicalAndFunctionServer::'<Inputs>k__BackingField'
    IL_001f: ldarg.0

但是我可以在該IL的哪里找到FunctionInput類的實例化。 我只能看到一些IFunctionInput類型,它是FunctionInput實現的接口,但是此代碼如何知道要實例化的實類型? 有人有主意嗎?

好的,我自己解決了。 尚未考慮到lambda正在捕獲“此”,因此它為其創建了一個關閉類。 Telerik的“ Just Decompile”沒有顯示Closure類,但是我已經在ildasm中看到了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM