如何使用 Reflection.emit 在运行时创建方法
我正在运行时使用反射发射创建一个对象。我成功创建了字段、属性和获取设置方法。 现在我想添加一个方法。为了简单起见,假设该方法仅返回一个随机数。如何定义方法体?
编辑:
是的,我一直在查看 msdn 文档以及其他参考资料,并且我开始关注这些东西。 我看到上面的示例是如何进行加法和/或乘法的,但是如果我的方法正在执行其他操作怎么办?我如何定义那个“东西” 假设我动态生成下面的类,我将如何创建 GetDetails() 方法的主体?
class TestClass
{
public string Name { get; set; }
public int Size { get; set; }
public TestClass()
{
}
public TestClass(string Name, int Size)
{
this.Name = Name;
this.Size = Size;
}
public string GetDetails()
{
string Details = "Name = " + this.Name + ", Size = " + this.Size.ToString();
return Details;
}
}
I'm creating an object at runtime using reflection emit. I successfully created the fields, properties and get set methods.
Now I want to add a method. For the sake of simplicity let's say the method just returns a random number. How do I define the method body?
EDIT:
Yes, I've been looking at the msdn documentation along with other references and I'm starting to get my head wrapped around this stuff.
I see how the example above is adding and/or multplying, but what if my method is doing other stuff. How do I define that "stuff"
Suppose I was generating the class below dynamically, how would I create the body of GetDetails() method?
class TestClass
{
public string Name { get; set; }
public int Size { get; set; }
public TestClass()
{
}
public TestClass(string Name, int Size)
{
this.Name = Name;
this.Size = Size;
}
public string GetDetails()
{
string Details = "Name = " + this.Name + ", Size = " + this.Size.ToString();
return Details;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 MethodBuilder 来定义方法。要定义方法主体,请调用 GetILGenerator() 来获取 < a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.aspx" rel="noreferrer">ILGenerator,然后调用 Emit 方法发出单个 IL 指令。 MSDN 文档中有一个 MethodBuilder 的示例,您可以在 使用反射发出页面:
听起来您正在寻找有关编写 MSIL 的资源。一项重要的资源是 OpCodes 类,它每个 IL 指令都有一个成员。该文档描述了每条指令的工作原理。另一个重要资源是 Ildasm 或 反射器。这些将使您看到已编译代码的 IL,这将帮助您了解要编写的 IL。通过 Reflector 运行 GetDetailsMethod 并将语言设置为 IL 会产生:
要动态生成这样的方法,您需要为每条指令调用 ILGenerator.Emit:
您可能还需要查找 MSIL 的介绍,例如以下内容: < a href="http://www.codeproject.com/KB/msil/il assembly.aspx" rel="noreferrer">IL 汇编语言简介。
You use a MethodBuilder to define methods. To define the method body, you call GetILGenerator() to get an ILGenerator, and then call the Emit methods to emit individual IL instructions. There is an example on the MSDN documentation for MethodBuilder, and you can find other examples of how to use reflection emit on the Using Reflection Emit page:
It sounds like you're looking for resources on writing MSIL. One important resource is the OpCodes class, which has a member for every IL instruction. The documentation describes how each instruction works. Another important resource is either Ildasm or Reflector. These will let you see the IL for compiled code, which will help you understand what IL you want to write. Running your GetDetailsMethod through Reflector and setting the language to IL yields:
To generate a method like that dynamically, you will need to call ILGenerator.Emit for each instruction:
You may also want to look for introductions to MSIL, such as this one: Introduction to IL Assembly Language.