具体实现
public object DLLReflection(string dllName, string nameSpace, string className, string functionName, object[] args, out string response)
{
try
{
if (-1 == dllName.LastIndexOf(".dll"))
{
dllName += ".dll";
}
var binPath = Path.GetFullPath(@"../../");
string dllPath = binPath + dllName;
if (!File.Exists(dllPath))
{
response = "dll path is not exist";
return false;
}
Assembly assembly = Assembly.LoadFrom(dllPath);
string loadType = nameSpace + "." + className;
Type type = assembly.GetType(loadType);
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod(functionName);
//if not have this method, continue execute
if (null == method)
{
response = "method is null iArg is" + iArg;
return false;
}
object value = method.Invoke(instance, args);
response = "";
return value;
}
catch (Exception ex)
{
response = ex.Message + "\n";
throw ex;
}
}
调用
private void InitializeCheck()
{
string response = "";
DllRefection reflection = new DllRefection();
var result = reflection.DLLReflection("UIH.Mcsf.Restore.Check.dll", "UIH.Mcsf.Restore.Check", "McsfRestoreCheck", "CheckHandler", new object[] { }, out response);
}