使用反射生成多个方法
我想知道如何使用反射生成多种类型方法。 示例:
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().. 来调用它们各自的核心方法吗?有没有更好的方法来完成这项工作?
I would like to know how I can generate multiple type methods using Reflection.
Example :
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()
}
My question, can I generate Method1(), Method2(), Method3().. dynamically to call their respective core methods. Is there a better way to get this done ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能值得研究一下IL重写库之一:
如果您无法弄清楚如何使其中之一为您工作,请查看手册
Reflection.Emit
。这篇codeproject 文章可能是最详细的来源。Probably worth looking into one of IL rewriting libraries:
If you can't work out how to make one of those work for you, have a look at manual
Reflection.Emit
. This codeproject article is probably the most detailed source doing it.使用T4 模板编写一个简单的代码生成器。这里有一些很好的资源。
Write a simple code-generator using T4 templates. Here are some good resources.