使用Reflection生成多个方法

时间:2010-07-15 06:20:40

标签: .net reflection.emit

我想知道如何使用Reflection生成多种类型的方法。 示例:

class A() {

 public void CoreMethod1() {

 }

 public void CoreMethod2() {

 }

 // .. 20 such core methods

 public void Method1() {
   //some initializations
   //call to CoreMethod1();
 }


 public void Method2() {
   //some initializations
   //call to CoreMethod2();
 }

 // i need 20 such methods which will call their respective CoreMethods

 //Method1(),Method2() are similar except for their call to the core method. i.e Every respective method will call its coremethod. Ex : Method1() -> CoreMethod1(), Method2() -> CoreMethod2()
}

我的问题是,我可以动态地生成Method1(),Method2(),Method3()..来调用它们各自的核心方法。有没有更好的方法来完成这项工作?

2 个答案:

答案 0 :(得分:2)

可能值得研究一下IL重写库:

如果您无法解决如何让其中一项适合您的工作,请查看手册Reflection.Emit。这个codeproject article可能是最详细的来源。

答案 1 :(得分:0)

相关问题