to ljj77:
有没有例子?

解决方案 »

  1.   

    yes, use delegates, for exampleusing System;class TestDel
    {
      public delegate void MyHandler(int i);  public static void Test(int i)
      {
    Console.WriteLine("static:{0}",i);
      }  public  void Test2(int i)
      {
    Console.WriteLine("instance:" + i);
      }  public static void Process(MyHandler mh)
      {
    mh(12);
      }  public static void Main()
      { TestDel td = new TestDel(); MyHandler mh = new MyHandler(TestDel.Test);
    mh += new MyHandler(td.Test2); Process(mh);
      }
      
    }
      

  2.   

    问了个傻问题不是同一个类之间如何运用代理?比如Test类需要调用其他类的函数
      

  3.   

    Test类需要调用其他类的函数
    ===
    直接调用啊
      

  4.   

    using System;class AnotherClass
    {
      public void Whatever(int i)
      {
    Console.WriteLine("AnotherClass : instance:" + i);
      }
    }class TestDel
    {
      public delegate void MyHandler(int i);  public static void Test(int i)
      {
    Console.WriteLine("static:{0}",i);
      }  public  void Test2(int i)
      {
    Console.WriteLine("instance:" + i);
      }  public static void Process(MyHandler mh)
      {
    mh(12);
      }  public static void Main()
      { TestDel td = new TestDel(); MyHandler mh = new MyHandler(TestDel.Test);
    mh += new MyHandler(td.Test2); AnotherClass ac = new AnotherClass();

    mh += new MyHandler(ac.Whatever); Process(mh);
      }
      
    }output:
    static:12
    instance:12
    AnotherClass : instance:12
      

  5.   

    using System;class AnotherClass
    {
      public void Whatever(int i)
      {
    Console.WriteLine("AnotherClass : instance:" + i);
      }
    }class TestDel
    {
      public delegate void MyHandler(int i);  public static void Test(int i)
      {
    Console.WriteLine("static:{0}",i);
      }  public  void Test2(int i)
      {
    Console.WriteLine("instance:" + i);
      }  public static void Process(MyHandler mh)
      {
    mh(12);
      }  public static void Main()
      { TestDel td = new TestDel(); MyHandler mh = new MyHandler(TestDel.Test);
    mh += new MyHandler(td.Test2); AnotherClass ac = new AnotherClass();

    mh += new MyHandler(ac.Whatever); Process(mh);
      }
      
    }output:
    static:12
    instance:12
    AnotherClass : instance:12
      

  6.   

    delegate void SortDelegate();public class MyClass 
    {
       //字符串排序
       public void StringSort() 
       {
          Console.WriteLine("排序字符串."); 
       }   //数组排序
       static public void ArrayMethod() 
       {
          Console.WriteLine("排序数组.");
       }
    }public class MainClass 
    {
       static public void Main() 
       {
          MyClass p = new MyClass();      MyDelegate d = new SortDelegate(StringSort);
          //调用委托
          d();      d = new SortDelegate(ArrayMethod);
          //调用委托,
          d();
       }
    }