//strHs 传递的函数
//obj传递的函数参数

using System;
using System.Runtime.InteropServices;

 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class XHDLL
 {

  public int ADD(int a ,int b)
  {
   return a+b;
  }

  public int MULTIP(int a,int b)
  {
   return a*b;
  }

 }


public string  FunctionCalc(string strHs,object[] obj)
  {
  string strClass ="XHDLL";  //类名

   Type t;
   object o;
   string str="";
   t=Type.GetType(strClass);


//一种方法得到该类下的所有方法然后调用
//   begion
//   System.Reflection.MethodInfo[] methods = t.GetMethods(); //得到所有的方法

//   for(int i=0;i<=methods.Length-1;i++)
//   {
//    if (strHs.ToUpper()==methods[i].Name.ToUpper())
//    {
//      o = System.Activator.CreateInstance(t);
//     str=Convert.ToString(methods[i].Invoke(o,obj));
//    }
//   }
//   end


   //二种方法直接调用
//   begin
   try
   {
    System.Reflection.MethodInfo method=t.GetMethod(strHs.ToUpper());
    o=System.Activator.CreateInstance(t);
    str=Convert.ToString(method.Invoke(o,obj));
   }
 
   catch(Exception ex)
   {
    str=ex.Message;
   }
//   end
   return str.ToString();
  
  }

//调用s

  private void button2_Click(object sender, System.EventArgs e)
  {
  
  object[] obj=new object[]{500,600};
   string str=FunctionCalc("add",obj);
   MessageBox.Show(str);
  }

  


相关文章:

  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-12-31
  • 2021-12-06
  • 2022-12-23
相关资源
相似解决方案