关闭

C#代理深入研究

发表于:2012-8-16 10:31

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:wangchongcy    来源:51Testing软件测试网采编

  本质上讲,代理提供了方法的封装,它把方法以某种方式封装在一个代理对象里,通过代理对象来执行调用方法、移除或者添加代理对象等操作。从另一种意义上讲,你可以把代理类型看作单一方法的接口,代理对象看作实现了该接口的对象。

  代理对象基础:代理类型

  下面的代码定义了一个代理类型

internal delegate void Feedback(Int32 value);

  C#编译器实际上将这个代理类型转化成为一个继承自MulticastDelegate的类。

internal class Feedback : System.MulticastDelegate
{
// Constructor
public Feedback(Object object, IntPtr method);
// Method with same prototype as specified by the source code
public virtual void Invoke(Int32 value);
// Methods allowing the callback to be called asynchronously
public virtual IAsyncResult BeginInvoke(Int32 value,
AsyncCallback callback, Object object);
public virtual void EndInvoke(IAsyncResult result);
}

  MulticastDelegate继承自Delegate类,下面是代理类的签名:

public abstract class Delegate : ICloneable, ISerializable
    {
        protected Delegate(object target, string method);
        protected Delegate(Type target, string method);

        public static bool operator !=(Delegate d1, Delegate d2);
        public static bool operator ==(Delegate d1, Delegate d2);

        public MethodInfo Method { get; }
        public object Target { get; }

        public virtual object Clone();
        public static Delegate Combine(params Delegate[] delegates);
        public static Delegate Combine(Delegate a, Delegate b);
        protected virtual Delegate CombineImpl(Delegate d);
        public static Delegate CreateDelegate(Type type, MethodInfo method);
        public static Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure);
        public static Delegate CreateDelegate(Type type, object firstArgument, MethodInfo method);
        public static Delegate CreateDelegate(Type type, object target, string method);
        public static Delegate CreateDelegate(Type type, Type target, string method);
        public static Delegate CreateDelegate(Type type, object firstArgument, MethodInfo method, bool throwOnBindFailure);
        public static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase);
        public static Delegate CreateDelegate(Type type, Type target, string method, bool ignoreCase);
        public static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure);
        public static Delegate CreateDelegate(Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure);
        public object DynamicInvoke(params object[] args);
        protected virtual object DynamicInvokeImpl(object[] args);
        public override bool Equals(object obj);
        public override int GetHashCode();
        public virtual Delegate[] GetInvocationList();
        protected virtual MethodInfo GetMethodImpl();
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context);
        public static Delegate Remove(Delegate source, Delegate value);
        public static Delegate RemoveAll(Delegate source, Delegate value);
        protected virtual Delegate RemoveImpl(Delegate d);
    }

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号