在.NET中,代理的内部实现是什么? [英] In .NET, what is the internal implementation of a delegate?

查看:136
本文介绍了在.NET中,代理的内部实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道代表的声明是这样的:

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);

编辑: 有些澄清。当您声明一个委托时,编译器自动会为您创建一个继承自System.MulticastDelegate的密封类。你可以看到这个,如果你用ildasm看你的程序集。这个整洁基本上,通过一个声明,您将在编译时为您生成一个完整的新类,并具有您需要的所有功能。

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.

推荐答案

所有代表都继承自 System.Delegate 类型其中包含目标方法。更确切地说,它们继承自 System.MultiCastDelegate ,它继承自 System.Delegate

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.

这篇关于在.NET中,代理的内部实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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