rt

解决方案 »

  1.   

    建议你看看delegate,也就是C#的委托
      

  2.   

    你的要求有2种方法实现
    1、方法的签名确定的情况,可以根据方法签名定义一个delegate,然后把delegate的实例当作参数传递,在需要的地方进行调用
    2、方法的签名不确定的情况,那就只有依据情况用反射来做了
      

  3.   

    http://www.webasp.net/article/12/11615.htm
    看看反射的概念
      

  4.   

    to:timmy3310(Tim) 、lovefootball(蟑螂(生活就是扯淡--做人要放低姿态)) ( )看了你所导的那篇文章,还是不能太明白反射的方法。你能不能就这个问题给一个很简短的说明(给一个小例子,请含关键代码,)指导一下我们初学者。多谢!
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace testApplication1
    {
        public class RefTest
        {
            public void Test(string s)
            {
                Console.WriteLine(s);
            }
        }
    }
    MethodInfo info = typeof(RefTest).GetMethod("Test");
    info.Invoke(new RefTest(), new object[] { "hello" });
      

  6.   

    关于反射,你可以自己Google一下慢慢看~~~~
      

  7.   

    有关把方法作为参数传递的写法总结(.net)方法定义:
    using System;namespace C_Win1
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Class1
    {
    public Class1()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public void Test1(string s)
    {
    Console.WriteLine(s);
    }
    public void Test2(string s)
    {
    Console.WriteLine(s);
    }
    }
    }
    1、反射:可以把方法名作为一个字符串来处理。写法灵活。代码实现如下:using System.Reflection;//使用反射来做//方法名字给一个ArrayList
    ArrayList MyMethodName =new ArrayList();
    MyMethodName.Clear();
    MyMethodName.Add("Test1");
    MyMethodName.Add("Test2");//定义反射
    MethodInfo info = null;//循环调用方法,这里把方法名作为一个字符串来处理
    for(int i=0;i<MyMethodName.Count;i++){   
       info=typeof(Class1).GetMethod(MyMethodName[i].ToString());
       info.Invoke(new Class1(), new object[] { "hello"+i.ToString() });
    }
    2、委托:方法签名确定,即在代码中要写死不能动态改变。代码实现如下:
     
    //定义委托类
    delegate void TestOp(String x);
    //注:“String x”被委托方法的输入参数; “void ”被委托方法的返回类型
    //注,这要写在类的外面,因为它相当于定义了一个新类。可写在namespace 下面,如:
    namespace C_Win1
    {
            //定义委托类
    delegate void TestOp(String x);
            ... private void Form1_Load(object sender, System.EventArgs e)
    {
    //委托,方法签名确定,即在代码中要写死不能动态改变 //实例化目标类
    Class1 myC=new Class1(); //实例化一个委托,把类Class1的方法Test1()给这个委托实例。实现方法被作为参数传递
    TestOp MyTOP=new TestOp(myC.Test1); //调用委托执行方法Test1()
    MyTOP("委托事件"); }
      

  8.   

    类、方法作为参数传递的写法整理。(通过反射实现)需要被调用的类及方法:
    public class Class1
    {
    public Class1()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public int Test1(String s)
    {
    return int.Parse(s)+1;
    }
    }调用其它类和方法的类:
    using System.Reflection;
    public class ReflectMethod
    {
    public ReflectMethod()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    /**参数:
     *   对象ClassName 是一个实例的对象
     *   类ClassName里面的方法MethodName名字的字符串
     *   方法MethodName的输入参数,这里设定为一个输入参数,并且为字符串
     *   方法MethodName的返回值设定为int类型
     * 
     *     本方法对类的传递是签名确定的,而类的方法的调用是对不确定的,即通过方法名的字符串来访问方法的
     * */
    public int DelMethod(Object ClassName,String MethodName,String Parameter_value)  
    {
    //反射
    MethodInfo info = null;

    //传入类的类型
    Type  mytype=ClassName.GetType(); //得到调用类(ClassName)的方法
    info =mytype.GetMethod(MethodName); //执行方法并得到返回值
    int mya=(int)info.Invoke(ClassName, new object[] { Parameter_value });
    return mya;
    }
    }主程序调用类Class1里面的方法的写法(通过类ReflectMethod的方法DelMethod)
                //实例化一个反射类
         ReflectMethod myRM=new ReflectMethod(); //实例化一个方法类
    Class1 myc=new Class1(); //通过反谢调用访求
    int t=myRM.DelMethod(myc,"Test1","6023"); //显示输出
    System.Diagnostics.Debug.WriteLine(t.ToString());