请教个问题:如何把一个函数名作为参数,调用函数的时候可以通过改变参数,调用不同的函数

解决方案 »

  1.   

    C#中的函數可以使用多載技術.即名稱相同, 參數不同.
    調用時, 系統會依參數個數及類別去自動匹配相就的函數.對於對態調用函數名稱, 可以使用委托
    比如BUTTON控件的CLICK動態使用函數CLICK()
    BUTTON.CLICK += NEW EventHandler(CLICK)即可.
      

  2.   

    對於對態調用函數名稱,   可以使用委托
    比如BUTTON控件的CLICK動態使用函數CLICK()
    BUTTON.CLICK   +=   NEW   EventHandler(CLICK)即可.对于这种情况,CLICK函数可否重载?
      

  3.   

    传委托啊
    这样public delegate void TestHandler();
    public class Test
    {
      public bool B{get;set;}//08语法
      public void Invoke(TestHandler th)
      {
        //写些条件啊这些比如:
        if(B)
          th.Invoke();//或直接写th();也一样
      }
      static void Main()
      {
        Console.WriteLine("test...");
        Test t=new Test();
        t.B=true;
        t.Invoke( t.TestM1);
        t.B=false;
        t.Invoke(t.TestM1);
        t.B=true;
        t.Invoke(t.TestM2);   
      }
      void TestM1(){Console.WriteLine("hellow world!");}
      void TestM2(){Console.WriteLine("thank you !");}
    }
      

  4.   

    用接口
    using System;
    using System.Collections;
    using System.Reflection;
    namespace SampleExmp
    {
        interface ItheFun
        {
            void theFun1();//方法1
        }
        class class1 : ItheFun
        {
            public void theFun1()
            {
                System.Console.WriteLine("方法1");
            }
        }
        class class2 : ItheFun
        {
            public void theFun1()
            {
                System.Console.WriteLine("方法2");
            }
        }
        class class3 : ItheFun
        {
            public void theFun1()
            {
                System.Console.WriteLine("方法3");
            }
        }
        class class4 : ItheFun
        {
            public void theFun1()
            {
                System.Console.WriteLine("方法4");
            }
        }
        class test
        {
            static void Main(string[] args)
            {
                class1 theClass1 = new class1();
                test theTest = new test();
                theTest.OutputData(theClass1);
            }
            public void OutputData(ItheFun mtheFun)
            {
                mtheFun.theFun1();
            }
        }
    }
    用迟绑定
    //csc /t:library /out myTest2.dll  目录/SampleExmp.cs
    using System;
    using System.Reflection;
    namespace theTest001
    {
        class testAssembly
        {
            public void UseReflection()
            {            Assembly a = Assembly.LoadFile(@"目录\myTest2.dll");
                Type theType = a.GetType("Class1");
                object theObj = Activator.CreateInstance(theType);
                MethodInfo theMethod = theType.GetMethod("theFun1");
                Type[] parame = new Type[0];
                theMethod.Invoke(theObj, parame);        }
        }
        class test
        {
            static void Main(string[] args)
            {
                testAssembly testA = new testAssembly();
                testA.UseReflection();
            }
        }
    }
    当然委托最好了
    直接是方法的抽象
    上面说了,不用重复