搞明白 委托 本质

委托是风,委托是雨,委托是唯一的神话。呵呵。

定义一个委托

public delegate int TakesAWhileDelegate(int data,int ms);在正统的.NET语言里这是个不伦不类的东西,去掉delegate关键字

public int TakesAWhileDelegate(int data,int ms);

就是个方法,加上个关键字就变成了一个委托。就这么一句,反射工具反射后会看到他的一些东西

2011022507232098.jpg

Base Types:基类 。一个正常的方法被加上一个delegate之后就成了一个委托,该方法名变成了一个类名,它的基类是System.MulticastDelegate,MuticastDelegate的基类是Delegate,先看System.MuticastDelegate类的定义,这个类有2个私有变量

IntPtr _invocationCount: 类型是整形指针类型,invocationCount表示调用次数。

object _invocationList:表示调用列表,类型是一个object。

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
      
[Serializable, ComVisible( true )]
public abstract class MulticastDelegate : Delegate
{
// Fields
private IntPtr _invocationCount;
private object _invocationList;

// Methods
// 构造函数,参数类型是object和string。
protected MulticastDelegate( object target, string method);
protected MulticastDelegate(Type target, string method);
// 将此委托与指定的委托合并以形成新的委托,follow表示要被合并的委托。
protected sealed override Delegate CombineImpl(Delegate follow);
[DebuggerNonUserCode] //DebuggerNonUserCode的属性,这表明这段代码将在调试运行时,调试器不会进入其中。
private void CtorClosed( object target, IntPtr methodPtr);
[DebuggerNonUserCode]
private void CtorClosedStatic( object target, IntPtr methodPtr);
[DebuggerNonUserCode]
private void CtorOpened( object target, IntPtr methodPtr, IntPtr shuffleThunk);
[DebuggerNonUserCode]
private void CtorRTClosed( object target, IntPtr methodPtr);
[DebuggerNonUserCode]
private void CtorSecureClosed( object target, IntPtr methodPtr, IntPtr callThunk, IntPtr assembly);
[DebuggerNonUserCode]
private void CtorSecureClosedStatic( object target, IntPtr methodPtr, IntPtr callThunk, IntPtr assembly);
[DebuggerNonUserCode]
private void CtorSecureOpened( object target, IntPtr methodPtr, IntPtr shuffleThunk, IntPtr callThunk, IntPtr assembly);
[DebuggerNonUserCode]
private void CtorSecureRTClosed( object target, IntPtr methodPtr, IntPtr callThunk, IntPtr assembly);
[DebuggerNonUserCode]
private void CtorSecureVirtualDispatch( object target, IntPtr methodPtr, IntPtr shuffleThunk, IntPtr callThunk, IntPtr assembly);
[DebuggerNonUserCode]
private void CtorVirtualDispatch( object target, IntPtr methodPtr, IntPtr shuffleThunk);
private object [] DeleteFromInvocationList( object [] invocationList, int invocationCount, int deleteIndex, int deleteCount);
private bool EqualInvocationLists( object [] a, object [] b, int start, int count);
public sealed override bool Equals( object obj);
public sealed override int GetHashCode();
public sealed override Delegate[] GetInvocationList();
protected override MethodInfo GetMethodImpl();
public override void GetObjectData(SerializationInfo info, StreamingContext context);
internal override object GetTarget();
private bool InvocationListEquals(MulticastDelegate d);
internal bool IsUnmanagedFunctionPtr();
internal MulticastDelegate NewMulticastDelegate( object [] invocationList, int invocationCount);
internal MulticastDelegate NewMulticastDelegate( object [] invocationList, int invocationCount, bool thisIsMultiCastAlready);
public static bool operator == (MulticastDelegate d1, MulticastDelegate d2);
public static bool operator != (MulticastDelegate d1, MulticastDelegate d2);
protected sealed override Delegate RemoveImpl(Delegate value); //从一个委托的调用列表中移除另一个委托的调用列表。
internal void StoreDynamicMethod(MethodInfo dynamicMethod);
[DebuggerNonUserCode]
private void ThrowNullThisInDelegateToInstance();
private bool TrySetSlot( object [] a, int index, object o);
}


Expand Methods

下面着重看几个比较重要的:

MulticastDelegate 是一个特殊类。编译器和其他工具可以从此类派生,但是您不能显式地从此类进行派生。Delegate 类也是如此。

    protected MulticastDelegate(object target, string method) : base(target, method)
    {
    }

什么是 target ,什么是 method,:base(target,method)又是什么意思?

举个例子:

public delegate int TakesAWhileDelegate(int data, int ms);

static int TakesAWhile(int data, int ms)
        {
            Console.WriteLine("begin");
            System.Threading.Thread.Sleep(ms);
            Console.WriteLine("completed");
            return ++data;
        }

 static void Main(string[] args)
        {

           TakesAWhileDelegate dl = TakesAWhile;

           dl.GetType();  //设置断点,运行。

        }
2011022508092246.jpg

可以看出,Method描述了一个方法,{Method={Int32 TakesAWhile(Int32,Int32)}}

2011022508233235.jpg

鼠标继续悬在base{System.Delegate}这一行。

_methodBase:类型是MethodBase类,提供有关方法和构造函数的信息。这边表示方法的信息{Int32 TakesAWhile(Int32,Int32)}

_methodPtr:这边表示方法的指针,IntPtr型。

_methodPtrAux:Aux辅助的意思。

Target什么时候不为null呢?

继续,public virtual Delegate[] GetInvocationList();

            public delegate void del(object sender, EventArgs e);
            public static event del evt;

            del handler = new del(method1);
            handler += new del(method2);
            foreach (del d in handler.GetInvocationList())
            {
                d(null, null);
            }

            //扩展下
            evt += new del(method1);
            evt += new del(method2);
            foreach (del d in evt.GetInvocationList())
            {
                d(null, null);
            }

        public static void method1(object sender, EventArgs e)
        { Console.WriteLine("方法一"); }
        public static void method2(object sender, EventArgs e)
        { Console.WriteLine("方法二"); }

结果是一致的。

posted on 2011-02-25 09:26  人的本质是什么? 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/PirateCaptain/articles/1964433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值