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

        }

    }

    例2:

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

        }

    }

     

     

     

  • 相关阅读:
    创建用户自定义函数 SQL
    sql with as 用法
    将string转为同名类名,方法名。(c#反射)
    linq 实现对象映射
    sql 游标
    C#编程总结(六)异步编程
    线程加锁解锁,用于循环条件不确定大小
    滚动条随代码滚动
    C# 代码小技巧
    reload方法
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2075483.html
Copyright © 2020-2023  润新知