Reflection.Emit如何为当前加载的程序集添加新方法. [英] Reflection.Emit How to add new methods for an assembly currently loaded.

查看:114
本文介绍了Reflection.Emit如何为当前加载的程序集添加新方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有兴趣知道如何使用C#中的Reflection.Emit进行以下操作.

我的程序中已经有一个程序集已加载,可以从中读取其中的所有类和方法.但是,我想在本届大会上增加更多方法.我了解创建新程序集,类和方法的过程,但是对于已经存在但具有新方法的程序集和类该怎么办.

我会很感激任何建议.

提前谢谢!

Peter

Hi All,

I am interested to know how to do the following with Reflection.Emit in C#.

I have an assembly in my program already loaded and from it I can read all the classes and methods inside of it. However, I would like to add more methods in this current Assembly. I understand the procedure for creating new assemblies, classes and methods, but what about having an assembly and classes already existing but with new methods.

I will apreciate any advice.

Thanks in advance!

Peter

推荐答案

一种方法是使用诸如System.Reflection.Emit.DynamicMethod这样有趣的东西.请参阅:
http://msdn.microsoft.com/en-us/library/system. Reflection.emit.dynamicmethod.aspx [ ^ ].

这个想法是:您不必创建被视为以某种类型声明的方法.实际上,从低层次上讲,即使在某个类或结构中声明的实例(非静态)方法也只有两个方面将其绑定到某种类型:1)它具有对其所有成员的完全访问权限,2)它具有对所有成员的完全访问权限.用于传递"this"(对类实例的引用)的第一个隐式参数.您可以使用动态方法来实现这两个功能,也可以使用(1)不使用(2)来实现奇特的功能-这是通过在某些构造函数中传递的标志skipVisibility来实现的(Owner.

此处描述:
http://msdn.microsoft.com/en-us/library/exczf7b9.aspx [ ^ ].

请注意定义和执行绑定到对象的动态方法"部分.

动态方法可以使用其Invoke方法直接调用,也可以使用CreateDelegate方法创建委托.重复调用动态方法的最快方法是调用此委托.一个问题是,如果要调用具有不同目标的委托,则应通过一次又一次地调用CreateDelegate来重新绑定它.这非常昂贵,而且要重复调用Invoke.解决此问题的一种方法是使用动态方法而不绑定到对象,而是显式传递引用,该引用也支持多态.要进行访问,您需要将skipVisibility与上面引用的构造函数一起使用.

现在,令人惊奇的是,Reflection不计算动态方法,至少不算作属于任何类型的方法!它们仅在运行时存在,因此它可能并不是您正在寻找的解决方案.但是,出于任何实际目的,它们与通过委托实例调用的任何常规"方法完全一样,并具有直接方法调用的良好性能.此外,在某些情况下,您可以创建高效的手动发出的代码,该代码具有Reflection特有的非凡灵活性,但不会降低性能!我强烈建议您尝试一下.如果您已经学习了如何发出代码(这是最难的部分),那么使用动态方法对您来说似乎相对容易.

—SA
One approach is using such an interesting thing as System.Reflection.Emit.DynamicMethod; please see:
http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx[^].

The idea is: you don''t have to create methods which are counted as being declared in certain type. In fact, on low level, even the instance (non-static) method declared in some class or a structure has only two aspects binding it to a certain type: 1) it has full access to all its members, 2) it has the first implicit parameter used to pass "this", the reference to the class instance. You can implement both features with dynamic methods, and also such an exotic thing as (1) without (2) — this is done via the flag skipVisibility passed in some constructors (http://msdn.microsoft.com/en-us/library/xc6e708b.aspx[^]), and passing "this" is implemented as adding the first parameter explicitly and using the parameter Owner.

This is described here:
http://msdn.microsoft.com/en-us/library/exczf7b9.aspx[^].

Pay attention for the section "To define and execute a dynamic method that is bound to an object".

The dynamic method can be called directly using its Invoke methods or by creation a delegate using CreateDelegate methods. The fastest way of calling the dynamic method repetitively is calling this delegate. One problem is that if you want to call the delegate with different targets, you should re-bind it by calling CreateDelegate again and again. This is very expensive, as well as repetitive call to the Invoke. One resolution of this problem is using dynamic method without binding to the object, but passing a reference explicitly, which also supports polymorphism. For access, you need to use skipVisibility with the constructor referenced above.

Now, the amazing thing is that Reflection does not count dynamic methods, at least not as methods belonging to any types! They exist only during run time, so it might not be exactly a solution you are looking for. However, for any practical purposes, they work exactly as any "regular" methods called through delegate instances, with decent performance of direct method calls. Besides, in certain situations you can create highly effective manually emitted code with extraordinary flexibility typical for Reflection but without its performance cost! I would highly recommend to try it out. If you already learned how to emit the code (which is the hardest part), using dynamic methods may seem relatively easy for you.

—SA


这篇关于Reflection.Emit如何为当前加载的程序集添加新方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆