public static object InvokeClass(string DllFileName, string ClassName, string MethodName, object[] ObjArrayParams)
{
try
{ // 载入程序集
Assembly assembly = Assembly.LoadFrom(DllFileName);
Type type = assembly.GetType(ClassName);
if (type == null)
{
throw new Exception("在“"+DllFileName+"”中不存在:" + ClassName);
}
// 查找要调用的方法并进行调用
MethodInfo m = type.GetMethod(MethodName);
if (m != null)
{
object o = Activator.CreateInstance(type);
return m.Invoke(o, ObjArrayParams);
}
else
{
throw new Exception("在“" + DllFileName + "”的类“" + ClassName + "”中不存在方法:" + MethodName);
}
}
catch (System.NullReferenceException e)
{
throw new Exception(e.Message);
//return (object)0;
}
}
上面是调用托管dll中的方法,但是,如果这个dll中有两外名称参数相同,返回类型不同的重载,那么我该怎么确定和控制具体调用哪一个?
MethodInfo m = type.GetMethod(MethodName,new Type[]{这里写入参数的各个Type});