在.NET中,委托的内部实现是什么?
我知道代表的声明是这样的:
public delegate int PerformCalculation(int x, int y);
但是,肯定还有更多的事情发生。委托的目的是提供指向方法的指针,为此,您可以在委托中封装对该方法的引用。
该引用保存在什么样的结构中(在委托内部)?我还了解您可以在委托中封装对多个方法的引用。这是否意味着委托中有一个数组保存这些?
另外,委托中定义了哪些方法等。当您使用简洁的声明委托时,到底发生了什么
public delegate int PerformCalculation(int x, int y);
?
编辑:一些澄清。当您声明委托时,编译器会自动创建一个继承自 System.MulticastDelegate 的密封类。如果您使用 ildasm 查看程序集,您可以看到这一点。这个整齐。基本上,通过一条语句,您将在编译时为您生成一个全新的类,并且它具有您需要的所有功能。
I understand that a declaration of a delegate is something like this:
public delegate int PerformCalculation(int x, int y);
However, there must be more going on. The purpose of the delegate is to provide a pointer to a method, and to do that you encapsulate the reference to the method in the delegate.
What kind of structure is this reference held in (internally in the delegate)? I also understand that you can encapsulate a reference to multiple methods in a delegate. Does this mean that there is an array in the delegate that holds these?
Also, what methods are defined in the delegate, etc. What is really happening when you declare a delegate with the terse:
public delegate int PerformCalculation(int x, int y);
?
EDIT: Some clarification. When you declare a delegate, the compiler automatically creates a sealed class that inherits from System.MulticastDelegate for you. You can see this if you look at your assembly with ildasm. This neat. Basically, with one statement, you are getting an entire new class generated for you at compile time, and it has all the functionality you need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在内部它是一个引用类型,与类非常相似。转录起来是这样的:
我将这些成员的实现留空,它们实际上映射到 CLR 中的代码。编译器根据委托声明的签名动态生成方法签名。请注意 x 和 y 参数。 JIT 编译器使用 += 或 -= 语法帮助调用构造函数,它知道委托目标方法的内存地址。编译器自动生成target参数值,具体取决于目标方法是否是静态的。这两个参数映射到 (Multicast)Delegate.Target 和 Method 属性。实际的基类实例可以是 Delegate 也可以是 MulticastDelegate,具体取决于订阅的目标数量。
这里有很多秘密酱汁。
Internally it's a reference type, quite similar to a class. Transcribed it looks like this:
I left the implementations of these members empty, they are actually mapped to code in the CLR. The compiler dynamically generates the method signatures, depending on the signature of the delegate declaration. Note the x and y arguments. The JIT compiler helps to get the constructor called, using the += or -= syntax, it knows the memory address of the delegate target method. The compiler automatically generated the target argument value, depending on whether the target method was static or not. The two arguments map to the (Multicast)Delegate.Target and Method properties. The actual base class instance can be either Delegate or MulticastDelegate, depending on how many targets were subscribed.
Lots of secret sauce going on here.
所有委托都继承自 System.Delegate 类型,该类型持有 < a href="http://msdn.microsoft.com/en-us/library/system.delegate.target.aspx" rel="noreferrer">目标 和 方法。更准确地说,它们继承自 System.MultiCastDelegate ,后者继承自
系统.委托
。All delegates inherit from the System.Delegate type which hold a Target and Method. More precisely they inherit from System.MultiCastDelegate which inherits from
System.Delegate
.