C#的Delegate的小心得

 
C# 的 Delegate Type
Delegate 是一种函数指针,但与普通的函数指针相比,区别主要有三:
1) 一个 delegate object 一次可以搭载多个方法(methods)[译注1],而不是一次一个。当我们唤起一个搭载了多个方法(methods)的 delegate,所有方法以其“被搭载到 delegate object 的顺序”被依次唤起——稍候我们就来看看如何这样做。
2) 一个 delegate object 所搭载的方法(methods)并不需要属于同一个类别。一个 delegate object 所搭载的所有方法(methods)必须具有相同的原型和形式。然而,这些方法(methods)可以即有 static 也有 non-static,可以由一个或多个不同类别的成员组成。
3) 一个 delegate type 的声明在本质上是创建了一个新的 subtype instance,该 subtype 派生自 .NET library framework 的 abstract base classes Delegate 或 MulticastDelegate,它们提供一组 public methods 用以询访 delegate object 或其搭载的方法(methods)
声明 Delegate Type
一个 delegate type 的声明一般由四部分组成:(a) 访问级别;(b) 关键字 delegate;(c)返回型别,以及该 delegate type 所搭载之方法的声明形式(signature);(d) delegate type 的名称,被放置于返回型别和方法的声明形式(signature)之间。(以上摘自网上)
C#虽然取消了指针的这个概念,但还是可以使用指针的,只要声明这段代码是非安全的(Unsafe).C#为指针找到一个更为有用的引用类型----代表元,它在C#类型里是十分安全的.在声明代表元时,只要指定代表元指向的原型的类型,它不能有返回值,也不能带回输出类型的参数.代表元可以封装一个静态方法,也可以封装一个非静态方法.例子如下:
例1.

using  System;
namespace  Delegate1
{    
    
public class MyClass
    
{
        
public int InstanceMethod(int a,int b)
        
{
            Console.WriteLine(
"Call InstanceMethod ");
            Console.WriteLine(
"First parametre is {0},second parametre is {1}", a, b);
            
return 0;
        }

        
static public int StaticMethod()
        
{
            Console.WriteLine(
"Call Static Method");
            
return 0 ;
        }

    }

    
class Class1
    
{
        
//声明的代表元要与将代表的方法的参数序列保持一致
        private delegate int MyDelegate();
        
private delegate int MeDelegatePara(int a, int b);

        [STAThread]
        
static void Main(string[] args)
        
{
            MyClass p 
= new MyClass();
            
//将代表元指向静态方法 StaticMethod
            MyDelegate D = new MyDelegate( MyClass.StaticMethod);
            D();
            
//将代表元指向带参数的非静态方法 InstanceMethod
            MeDelegatePara d = new MeDelegatePara( p.InstanceMethod );
            d(
12);            
        }

    }

}

例1的缺点在于每次使用代表元时都要进行实例化,如果能从类中获取代表元就方便了很多,解决方法是将代表元的声明放入到高一级的名字空间
中,或者将代表元封装到一个类中,然后在相关类中以属性的形式绑定静态代表.我推荐使用类封装的方式,下面是改进后的类结构.
 


using  System;
namespace  Delegate1
{    
        
public class CDelegate
    
{
        
//声明的代表元与将代表的方法的参数的序列要保持一致
        public delegate int MyDelegate();
        
public delegate int MeDelegatePara(int a, int b);
    }

 
    
public class MyClass
    
{
        
          
public int InstanceMethod(int a,int b)
        
{
            Console.WriteLine(
"Call InstanceMethod ");
            Console.WriteLine(
"First parametre is {0},second parametre is {1}", a, b);
            
return 0;
        }

        
static public int StaticMethod()
        
{
            Console.WriteLine(
"Call Static Method");
            
return 0 ;
        }

        
        
public static CDelegate.MyDelegate  MyDelegate1
        
{
            
get 
            
{
                
return new CDelegate.MyDelegate(StaticMethod);
            }

        }

        
        
public  CDelegate.MeDelegatePara  MyDelegate2
        
{
            
get
            
{
                
return new CDelegate.MeDelegatePara(InstanceMethod);
            }
 
        }

    }

    

    
class Class1
    
{
        
        [STAThread]
        
static void Main(string[] args)
        
{
            MyClass p 
= new MyClass();
            
//将代表元指向静态方法 StaticMethod
            CDelegate.MyDelegate D = MyClass.MyDelegate1;
            D();
            
//将代表元指向带参数的非静态方法 InstanceMethod
            
            CDelegate.MeDelegatePara d 
= p.MyDelegate2   ;
            d(
12);            
            
        }

    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值