可以看出它默认继承自System.MulticastDelegate[所有委托都继承此类,MulticastDelegate又继承自System.Delegate],我们声明的public,所以编译器产生的类也是 Int32 一个内部的整数值,CLR用它来标识回调函数 _prev System.MulticastDelegate 指向另一个委托对象 所有委托都有这样一个构造器[void .ctor (object MulticastDelegate又重写了Delegate的Equals方法,它又加了一项比较,就是_prev字段。 说白点就是Delegate的Equals判断一个委托对象是否相等,MulticastDelegate的Equals则在Delegate的基础上又增加委托链表的判断。 委托链[_prev]: 每一个MulticastDelegate对象都有一个_prev字段,指向另一个MulticastDelegate对象的引用,则可以构成一个链表。
(I).继承了System.MulticastDelegate。 (II).包含了构造方法、BeginInvoke、EndInvoke、Invoke方法。 也就是说此时,delegate代码已经编译成了如下代码: 编译后: class DGSayHi:System.MulticastDelegate { public DelegateForFun(); string value,AsyncCallback callback,Object object); void EndInvoke(IAsyncResult result); } (3)System.MulticastDelegate 类 下面我们来看看借助.Net Reflector工具来查看类库中的 MulticastDelegate 类 public abstract class MulticastDelegate : Delegate 由此我们可以看出继承关系:DGSayHi –> MulticastDelegate–> Delegate MulticastDelegate类中有3个重要的成员,其中两个继承自 Delegate :
多播委托(MulticastDelegate)提供了一种类似于流水线式的钩子机制,只要加载到这条流水线上的委托,都会被顺序执行。 因为所有的委托都继承自MulticastDelegate,因此所的委托都具备多播特性。 MyMulticastDelegate(SecondMethod); ////将这两个方法合并到一个多播委托中 MyMulticastDelegate multicastDelegate = MyFirst; //在多播委托的调用链中添加新的委托元素 multicastDelegate = multicastDelegate + MySecond ; //调用多播委托,并且序列执行两个委托所指向的方法 multicastDelegate("Test"); Console.Read
在FCL中,所有的委托类型都派生自MulticastDelegate,该类型在System.MulticastDelegate类型中。 return b; return a.CombineImpl(b); } 以上两个方法为System.Delegate类型中,CombineImpl方法在MulticastDelegate dFollow = (MulticastDelegate)follow; Object[] resultList; int followCount = v = value as MulticastDelegate; if (v == null) return this; innerDelegate = _invocationList as MulticastDelegate; if (innerDelegate !
大概是这样,伪代码如下: public class FooHandle: MulticastDelegate { public FooHandle(object @object,IntPtr menthod 提供的特殊方法 // 编译后公共语言运行时给MulticastDelegate提供的特殊方法 void BeginInvoke(int value,System.AsyncCallback callback , object obj) } 我们可以看编译后FooHandle就是一个类,且继承MulticastDelegate,且继承链关系在msdn是这样的: ? multicastDelegate = (MulticastDelegate)follow; int num = 1; object[] array = multicastDelegate = null) { num = (int)multicastDelegate.
本文对事件的分析可以概括为下面三句话: 事件本质上是一个MulticastDelegate对象; MulticastDelegate对象是多个Delegate对象的链表; Delegate = Object 二、MulticastDelegate对象多个Delegate对象的链表 其实我们平时讲的委托,并不是一个单个的Delegate对象,实际上是一个委托链,这样一个委托链通过MulticastDelegate 三、事件本质上是一个MulticastDelegate对象 我们使用的事件一般通过EventHandler或者System.EventHandler<TEventArgs>表示,其本质来时一个通过MulticastDelegate
所有的委托类型都是从System.MulticastDelegate类隐式派生的。而System.MulticastDelegate继承自System.Delegate。
(所以被event修饰的多路广播委托[MulticastDelegate]才被称“事件”[这个事件并不是关键字event,而是MulticastDelegate的一个特殊实例]) Delegate (类 -> System命名空间下 [程序集mscorlib]) MulticastDelegate(类-> System命名空间下 [程序集mscorlib]) EventHandler(委托 [他不是类 Object(这个继承是IL层面的,C#层面是看不到的所以不用太在意) 里面有很多方法,不过我们可能能使用的也只有public的方法而已,而且这是一个抽象类我们也是无法直接实例化的(下面的同样也是) 再来看看MulticastDelegate 很明显MulticastDelegate同样是个类而且直接继承与上面的Delegate 还多了2个私有字段 _invocationCount , _invocationList ,以及一些新的方法 由此可以看出这 MulticastDelegate,跟Delegate,虽然名字很有迷惑性,但是他们不是委托,是class(下面会提到这个class跟我们委托之间的关系),而且正常情况下我们还用不到,不过经常有人把Delegate
MyMulticastDelegate MyFirst = new MyMulticastDelegate(MethodTest); MyMulticastDelegate multicastDelegate = MyFirst; multicastDelegate("Test"); //////////////////////////////////////////
1、面向对象:.net把委托(函数指针)定义为类型,具体的类名为委托名称,并且存在继承关系:自定义委托<--System.MulticastDelegate<--System.Delegate。 添加(移除)函数指针实际调用了MulticastDelegate的CombineImpl方法把函数指针添加到_invocationList(类型为object数组),可以同过委托实例.GetInvocationList
这里就需要跟随邹老师的博文走走了,通过VS中自带的MSIL反编译程序,将生成后的.exe拖到工具中查看委托类型声明的代码,发现其编译前就生成了一个类;它继承了System.MulticastDelegate 另外MulticastDelegate则继承自Delegate类。 通过Reflector反编译工具,可以看出:继承关系:编译前生成的类 –> MulticastDelegate–> Delegate,而MulticastDelegate类中有3个重要的成员,其中两个继承自
int b); MyDelegate md =null; 既然叫做委托类型,就说明MyDelegate实际上是一个类,上面的写法只是一种简单的缩略写法,实际上,我们自己定义的委托,都是继承自System.MulticastDelegate MulticastDelegate类 MultiDelegate类是一个特殊类(Special Class),和System.Delegate类一样,该类只能够被编译器以及内置的工具类所继承,我们自定义的类是不能够显式的继承自该类的
getValue() { return value; } } public static void main(String[] args) throws Exception { MulticastDelegate multicastDelegate = MulticastDelegate.create(DelegateProvider.class); MulticastBean first = new MulticastBean (); MulticastBean second = new MulticastBean(); multicastDelegate = multicastDelegate.add(first); multicastDelegate = multicastDelegate.add(second); DelegateProvider provider = (DelegateProvider ) multicastDelegate; provider.setValue("throwable"); System.out.println(first.getValue()); System.out.println
委托Delegate 继承自MulticastDelegate 声明委托定义签名: public delegate int DemoDelegate(int num1, int num2);
DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate
但是并不是直接继承自System.Delegate,而是继承自System.MulticastDelegate,MulticastDelegate派生于Delegate。 MulticastDelegate不涉及到本篇文章的主题,在这里就不再赘言介绍了。
自定义委托继承关系是:System.MulticastDelegate —> System.Delegate —>System.Object。 自定义委托的基类就是多播委托MulticastDelegate ,这就要看看微软是如何对System.MulticastDelegate定义的: MulticastDelegate拥有一个带有链接的委托列表
delegate void MyDelegate(string text); C#编译器会生成完整代理类(可通过SharpLab.io验证): public sealed class MyDelegate : MulticastDelegate callback, object @object); public virtual void EndInvoke(IAsyncResult result); } 核心要点: • 继承自MulticastDelegate
所有委托类型都派生自MulticastDelegate MulticastDelegate重要的非公共字段 Feedback fbstatic = new Feedback(Program.FeedbackToConsole (P356 last2)(如开头demo2函数所示) MulticastDelegate类提供了一个实例方法GetInvocationList,用于显式调用链中的每一个委托,并允许你使用需要的任何算法。 (P357 1)示例:(建议认真阅读,原文太长这里就不放了)(P357 last) public abstract class MulticastDelegate : Delegate { //
一, 委托 用IL 分析可看出Delegate继承自System.MulticastDelegate类,并自动生成BeginInvoke,EndInvoke,Invoke三种常见的方法。 Console.WriteLine(message); 21 Console.ReadKey(); 22 } 23 } 1.3 多路广播委托 在上面提过,委托类继承于MulticastDelegate