![](/img/trans.png)
[英]Can I use System.Reflection.Emit to build a constructor for an existing type?
[英]Can I add a dynamic method to an existing type using System.Reflection.Emit?
我剛開始學習Emit命名空間 - 以下是否有效? 這引發了一個異常:
public class EmitTest
{
public void Test()
{
DynamicMethod dynMeth = new DynamicMethod("Foo", null, null, typeof(EmitTest));
ILGenerator gen = dynMeth.GetILGenerator();
gen.EmitWriteLine("Foo");
gen.Emit(OpCodes.Ret);
dynMeth.Invoke(null, null);
dynamic d = this;
d.Foo();
}
}
反正有沒有按照預期進行這項工作,還是DLR的限制? 在這里,我創建了一個新的void方法Foo(),並將其創建為EmitTest類的成員。 運行時說EmitTest上找不到Foo()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.