永远的学生

qindgfly

导航

C#中的委托

  1. 委托的定义

    委托是一种定义方法签名的类型,可以与具有兼容签名的任何方法关联。您可以通过委托调用方法。委托用于将方法作为参数传递给其他方法。事件处理程序就是通过委托调用的方法。您可以创建一个自定义方法,当发生特定事件时某个类(例如 Windows 控件)就可以调用您的方法。下面的示例演示了一个委托声明:

    委托

    public delegate int PerformCalculation(int x, int y);

     

     与委托的签名(由返回类型和参数组成)匹配的任何可访问类或结构中的任何方法都可以分配给该委托。方法可以是静态方法,也可以是实例方法。这样就可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,就可以分配您自己的方法。

    说明:

    在方法重载的上下文中,方法的签名不包括返回值。但在委托的上下文中,签名的确包括返回值。换句话说,方法和委托必须具有相同的返回值。

     将方法作为参数进行引用的能力使委托成为定义回调方法的理想选择。例如,可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。

     委托具有以下特点:

    • 委托类似于 C++ 函数指针,但它们是类型安全的。

    • 委托允许将方法作为参数进行传递。

    • 委托可用于定义回调方法。

    • 委托可以链接在一起;例如,可以对一个事件调用多个方法。

    • 方法不必与委托签名完全匹配(协变和逆变)。

    C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。C# 3.0 引入了 Lambda 表达式,利用它们可以更简练地编写内联代码块。匿名方法和 Lambda 表达式(在某些上下文中)都可编译为委托类型。这些功能统称为匿名函数。


  2. 带有命名方法的委托与带有匿名方法的委托
    可以与命名方法关联。使用命名方法对委托进行实例化时,该方法将作为参数传递,例如:
    // Declare a delegate:
    delegate void Del(int x);

    // Define a named method:
    void DoWork(int k) { /* ... */ }

    // Instantiate the delegate using the method as a parameter:
    Del d = obj.DoWork;

     

    这被称为使用命名的方法。使用命名方法构造的委托可以封装静态方法或实例方法。在早期版本的 C# 中,命名方法是对委托进行实例化的唯一方式。但是,在不希望付出创建新方法的系统开销时,C# 使您可以对委托进行实例化,并立即指定委托在被调用时将处理的代码块。代码块可以包含 lambda 表达式或匿名方法。

    作为委托参数传递的方法必须与委托声明具有相同的签名。

    委托实例可以封装静态或实例方法。

    尽管委托可以使用 out 参数,但建议您不要将其用于多路广播事件委托,因为您无法知道哪个委托将被调用。

    以下是声明及使用委托的一个简单示例。注意,委托 Del 和关联的方法 MultiplyNumbers 具有相同的签名

    // Declare a delegate:
    delegate void Del(int x);

    // Define a named method:
    void DoWork(int k) { /* ... */ }

    // Instantiate the delegate using the method as a parameter:
    Del d = obj.DoWork;

     

     


    在下面的示例中,一个委托被同时映射到静态方法和实例方法,并分别返回特定的信息。
    静态方法和示例方法
    // Declare a delegate
    delegate void Del();

    class SampleClass
    {
        
    public void InstanceMethod()
        {
            System.Console.WriteLine(
    "A message from the instance method.");
        }

        
    static public void StaticMethod()
        {
            System.Console.WriteLine(
    "A message from the static method.");
        }
    }

    class TestSampleClass
    {
        
    static void Main()
        {
            SampleClass sc 
    = new SampleClass();

            
    // Map the delegate to the instance method:
            Del d = sc.InstanceMethod;
            d();

            
    // Map to the static method:
            d = SampleClass.StaticMethod;
            d();
        }
    }
    /* Output:
        A message from the instance method.
        A message from the static method.
    */

     

     


    下面的示例中,使用匿名方法来对委托进行实例化,并调用

    匿名方法实例化委托示例
    using System;
    // Declare delegate -- defines required signature:
    delegate void SampleDelegate(string message);

    class MainClass
    {
        
    static void Main()
        {
            
    // Instantiate delegate with anonymous method:
            SampleDelegate d = delegate(string message)
            { 
                Console.WriteLine(message); 
            };

            
    // Invoke delegate d:
            d("Hello");
        }
    }

     

    下面的示例中,将Lambda表达式分配给委托,并调用

     

    delegate int del(int i);
    del myDelegate 
    = x => x * x;
    int j = myDelegate(5); //j = 25

     

     


     

  3. 委托中的协变和逆变
    将方法签名与

    委托

    类型匹配时,协变逆变可以提供一定程度的灵活性。
    协变允许方法具有的派生返回类型比委托中定义的更多。
    下面的示例演示如何将委托与具有返回类型的方法一起使用,这些返回类型派生自委托签名中的返回类型。由 SecondHandler 返回的数据类型是 Dogs 类型,它是由委托中定义的 Mammals 类型派生的。
    协变
    class Mammals
    {
    }

    class Dogs : Mammals
    {
    }

    class Program
    {
        
    // Define the delegate.
        public delegate Mammals HandlerMethod();

        
    public static Mammals FirstHandler()
        {
            
    return null;
        }

        
    public static Dogs SecondHandler()
        {
            
    return null;
        }

        
    static void Main()
        {
            HandlerMethod handler1 
    = FirstHandler;

            
    // Covariance allows this delegate.
            HandlerMethod handler2 = SecondHandler;
        }
    }

     


    逆变允许方法具有的派生参数类型比委托类型中的更少。
    下面示例演示如何将委托与具有某个类型的参数的方法一起使用,这些参数是委托签名参数类型的基类型。通过逆变,以前必须使用若干个不同处理程序的地方现在只要使用一个事件处理程序即可 。如,现在可以创建一个接收 EventArgs 输入参数的事件处理程序,然后,可以将该处理程序与发送 MouseEventArgs 类型(作为参数)的 Button.MouseClick 事件一起使用,也可以将该处理程序与发送 KeyEventArgs 参数的 TextBox.KeyDown 事件一起使用。
    逆变
    System.DateTime lastActivity;
    public Form1()
    {
        InitializeComponent();

        lastActivity 
    = new System.DateTime();
        
    this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs
        this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs

    }

    // Event hander for any event with an EventArgs or
    // derived class in the second parameter
    private void MultiHandler(object sender, System.EventArgs e)
    {
        lastActivity 
    = System.DateTime.Now;
    }

     



  4. 委托和接口

    委托和接口都允许类设计器分离类型声明和实现。任何结构都能继承和实现给定的接口。可以为任何类上的方法创建委托,前提是该方法符合委托的方法签名。接口引用或委托可由不了解实现该接口或委托方法的类的对象使用。既然存在这些相似性,那么类设计器何时应使用委托,何时又该使用接口呢?

    在以下情况下,请使用委托:

    • 当使用事件设计模式时。

    • 当封装静态方法可取时。

    • 当调用方不需要访问实现该方法的对象中的其他属性、方法或接口时。

    • 需要方便的组合。

    • 当类可能需要该方法的多个实现时。

    在以下情况下,请使用接口:

    • 当存在一组可能被调用的相关方法时。

    • 当类只需要方法的单个实现时。

    • 当使用接口的类想要将该接口强制转换为其他接口或类类型时。

    • 当正在实现的方法链接到类的类型或标识时:例如比较方法。

    使用单一方法接口而不使用委托的一个很好的示例是 IComparable 或泛型版本 IComparable(T)IComparable 声明 CompareTo 方法,该方法返回一个整数,指定相同类型的两个对象之间的小于、等于或大于关系。IComparable 可用作排序算法的基础。虽然将委托比较方法用作排序算法的基础是有效的,但是并不理想。因为进行比较的能力属于类,而比较算法不会在运行时改变,所以单一方法接口是理想的。

  5. 委托的合并(多路广播委托)

    委托

    象的一个用途在于,可以使用 + 运算符将它们分配给一个要成为多路广播委托的委托实例。组合的委托可调用组成它的那两个委托。只有相同类型的委托才可以组合。

    - 运算符可用来从组合的委托移除组件委托。

    委托的合并
    delegate void Del(string s);

    class TestClass
    {
        
    static void Hello(string s)
        {
            System.Console.WriteLine(
    "  Hello, {0}!", s);
        }

        
    static void Goodbye(string s)
        {
            System.Console.WriteLine(
    "  Goodbye, {0}!", s);
        }

        
    static void Main()
        {
            Del a, b, c, d;

            
    // Create the delegate object a that references 
            
    // the method Hello:
            a = Hello;

            
    // Create the delegate object b that references 
            
    // the method Goodbye:
            b = Goodbye;

            
    // The two delegates, a and b, are composed to form c: 
            c = a + b;

            
    // Remove a from the composed delegate, leaving d, 
            
    // which calls only the method Goodbye:
            d = c - a;

            System.Console.WriteLine(
    "Invoking delegate a:");
            a(
    "A");
            System.Console.WriteLine(
    "Invoking delegate b:");
            b(
    "B");
            System.Console.WriteLine(
    "Invoking delegate c:");
            c(
    "C");
            System.Console.WriteLine(
    "Invoking delegate d:");
            d(
    "D");
        }
    }
    /* Output:
    Invoking delegate a:
      Hello, A!
    Invoking delegate b:
      Goodbye, B!
    Invoking delegate c:
      Hello, C!
      Goodbye, C!
    Invoking delegate d:
      Goodbye, D!
    */

     

     

     

  6. 委托编程示例

    下面的示例阐释声明、实例化和使用委托。BookDB 类封装一个书店数据库,它维护一个书籍数据库。它公开 ProcessPaperbackBooks 方法,该方法在数据库中查找所有平装书,并对每本平装书调用一个委托。使用的 delegate 类型名为 ProcessBookDelegateTest类使用该类打印平装书的书名和平均价格。

    委托的使用促进了书店数据库和客户代码之间功能的良好分隔。客户代码不知道书籍的存储方式和书店代码查找平装书的方式。书店代码也不知道找到平装书后将对平装书执行什么处理。

    委托编程示例
    // A set of classes for handling a bookstore:
    namespace Bookstore
    {
        
    using System.Collections;

        
    // Describes a book in the book list:
        public struct Book
        {
            
    public string Title;        // Title of the book.
            public string Author;       // Author of the book.
            public decimal Price;       // Price of the book.
            public bool Paperback;      // Is it paperback?

            
    public Book(string title, string author, decimal price, bool paperBack)
            {
                Title 
    = title;
                Author 
    = author;
                Price 
    = price;
                Paperback 
    = paperBack;
            }
        }

        
    // Declare a delegate type for processing a book:
        public delegate void ProcessBookDelegate(Book book);

        
    // Maintains a book database.
        public class BookDB
        {
            
    // List of all books in the database:
            ArrayList list = new ArrayList();

            
    // Add a book to the database:
            public void AddBook(string title, string author, decimal price, bool paperBack)
            {
                list.Add(
    new Book(title, author, price, paperBack));
            }

            
    // Call a passed-in delegate on each paperback book to process it: 
            public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
            {
                
    foreach (Book b in list)
                {
                    
    if (b.Paperback)
                        
    // Calling the delegate:
                        processBook(b);
                }
            }
        }
    }


    // Using the Bookstore classes:
    namespace BookTestClient
    {
        
    using Bookstore;

        
    // Class to total and average prices of books:
        class PriceTotaller
        {
            
    int countBooks = 0;
            
    decimal priceBooks = 0.0m;

            
    internal void AddBookToTotal(Book book)
            {
                countBooks 
    += 1;
                priceBooks 
    += book.Price;
            }

            
    internal decimal AveragePrice()
            {
                
    return priceBooks / countBooks;
            }
        }

        
    // Class to test the book database:
        class TestBookDB
        {
            
    // Print the title of the book.
            static void PrintTitle(Book b)
            {
                System.Console.WriteLine(
    "   {0}", b.Title);
            }

            
    // Execution starts here.
            static void Main()
            {
                BookDB bookDB 
    = new BookDB();

                
    // Initialize the database with some books:
                AddBooks(bookDB);

                
    // Print all the titles of paperbacks:
                System.Console.WriteLine("Paperback Book Titles:");

                
    // Create a new delegate object associated with the static 
                
    // method Test.PrintTitle:
                bookDB.ProcessPaperbackBooks(PrintTitle);

                
    // Get the average price of a paperback by using
                
    // a PriceTotaller object:
                PriceTotaller totaller = new PriceTotaller();

                
    // Create a new delegate object associated with the nonstatic 
                
    // method AddBookToTotal on the object totaller:
                bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

                System.Console.WriteLine(
    "Average Paperback Book Price: ${0:#.##}",
                        totaller.AveragePrice());
            }

            
    // Initialize the book database with some test books:
            static void AddBooks(BookDB bookDB)
            {
                bookDB.AddBook(
    "The C Programming Language""Brian W. Kernighan and Dennis M. Ritchie"19.95mtrue);
                bookDB.AddBook(
    "The Unicode Standard 2.0""The Unicode Consortium"39.95mtrue);
                bookDB.AddBook(
    "The MS-DOS Encyclopedia""Ray Duncan"129.95mfalse);
                bookDB.AddBook(
    "Dogbert's Clues for the Clueless""Scott Adams"12.00mtrue);
            }
        }
    }
    /* Output:
    Paperback Book Titles:
       The C Programming Language
       The Unicode Standard 2.0
       Dogbert's Clues for the Clueless
    Average Paperback Book Price: $23.97
    */

     

     

     

     

 


posted on 2010-03-02 22:36  东国先生  阅读(510)  评论(0编辑  收藏  举报