TextBox1.Text = xxx1(string str);
TextBox1.Text =xxx2(string str);
TextBox1.Text =xxx3(string str);
要频繁测试这些函数x1,x2,x3....能不能弄成动态的
string Nxxx=TextBox1.Text
TextBox2.Text=Nxxx(string str);或想个好点的办法别加个函数就改代码测试,麻烦的要死。。

解决方案 »

  1.   

    就是经常增加函数xxx1(string str); xxx2(string str); 。
    增加的函数都要经过测试,测试方法
    TextBox2.Text=xxx1(string str); TextBox2.Text=xxx2(string str); 。这样我没增加一个函数就要重新改代码测试xxx1,xxx2,xxx3....
    能不能把函数名做成能输入的,传递两个参数就能测试新的函数TextBox2.Text=Nxxx1(string str); 函数名Nxxx1作为参数传递过去
      

  2.   

    建议看看动态编译的东东:System.Object
      System.MarshalByRefObject
        System.ComponentModel.Component
          System.CodeDom.Compiler.CodeDomProvider
            Microsoft.VisualC.CppCodeProvider
            Microsoft.CSharp.CSharpCodeProvider
            Microsoft.JScript.JScriptCodeProvider
            Microsoft.VisualBasic.VBCodeProvider
      

  3.   

    那位贴点代码。delegate的如果有返回值怎么搞。。
      

  4.   

    代理不行吧?
    //第一步,声明一个delegate对象
        public delegate void Mydelegate(string mydelegate);
        protected void Page_Load(object sender, EventArgs e)
        {
            //第三步 产生一个delegate对象的时候,把你刚刚实现的函数(方法)作为参数传给他的构造函数。
            Mydelegate hello = new Mydelegate(Hello);
            hello("hello");
            Mydelegate statichello = new Mydelegate(staticHello);
            statichello("beijing");    }
        //第二步 实现和delegate具有相同参数和返回值的函数实现(非静态的)
        //这里的参数是mydelegate,返回值是void 
        public void Hello(string mydelegate)
        {
            Response.Write(mydelegate);
        }
        //第二步 实现和delegate具有相同参数和返回值的函数实现(静态的)
        //这里的参数是mydelegate,返回值是void
        public static void staticHello(string mydelegate)
        {
            HttpContext.Current.Response.Write(mydelegate);
        }
      

  5.   

    用反射:
    MethodInfo method = Type.GetType("Program").GetMethod(methodName);
    string t = (string)method.Invoke(program, new object[]{ arg });  
    using System; 
    using System.Reflection; class Program
    {
      public string N001(string s)
      {
        return "Hello, " + s;
      }  public string N002(string s)
      {
        return "This is N002, " + s;
      }  public string N003(string s)
      {
        return "I'm N003, " + s;
      }
      
      static void Main()
      {
        Program program = new Program();
        string arg = "ABC";
        foreach (string methodName in new string[]{ "N001", "N002", "N003" })
        {
          MethodInfo method = Type.GetType("Program").GetMethod(methodName);
          string t = (string)method.Invoke(program, new object[]{ arg });  
          Console.WriteLine(t); 
        }
      }
    }
    /* 程序输出:
    Hello, ABC
    This is N002, ABC
    I'm N003, ABC
    */
      

  6.   

    using System; 
    using System.Reflection; class Program
    {
      public string N001(string s)
      {
        return "Hello, " + s;
      }  public string N002(string s)
      {
        return "This is N002, " + s;
      }  public string N003(string s)
      {
        return "I'm N003, " + s;
      }
    }class Test
    {  
      static void Main()
      {
        Program program = new Program();
        string arg = "ABC";
        foreach (string method in new string[]{ "N001", "N002", "N003" })
        {
          string t = (string)Type.GetType("Program").GetMethod(method).Invoke(program, new object[]{arg});  
          Console.WriteLine(t); 
        }
      }
    }
    /* 程序输出:
    Hello, ABC
    This is N002, ABC
    I'm N003, ABC
    */
      

  7.   

    不用发射的话,
    可以使用Microsoft的企业库中的测试库
    也很方便。