//调用
private string InvokeMethod(object[] paramArray, string method)
{
string[] methodArray = method.Split(',');
MethodInfo methodInfo = null;
///动态程序集
var dynamicAssembly = DynamicAssemblyHelp.buildCodeFile();
object objHelloWorld = dynamicAssembly.CreateInstance(methodArray[0]);
if (objHelloWorld != null)
{
methodInfo = objHelloWorld.GetType().GetMethod(methodArray[1]);
if (methodInfo != null)
{
return (string)methodInfo.Invoke(null, paramArray);
}
}
///当前程序集
Type type = Type.GetType(methodArray[0]);
MethodInfo[] methos = type.GetMethods();
methodInfo = type.GetMethod(methodArray[1]);
if (methodInfo != null)
{
string newParam = (string)methodInfo.Invoke(null, paramArray);
return newParam;
}
return string.Empty;
}
/// <summary>
/// 编译-返回Assembly(判断文件是否改动避免重复加载)
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static Assembly buildCodeFile()
{
string path = AppDomain.CurrentDomain.BaseDirectory + "DynamicCs";
DirectoryInfo directoryInfo = new DirectoryInfo(path);
var LastWriteTime = directoryInfo.GetFiles().Max(m => m.LastWriteTime);
if (updateFileTime==null || LastWriteTime > updateFileTime)
{
string pahtnext = $"{path}\\ProcessDutScript.cs";
List<string> codelist = new List<string>();
codelist.Add(pahtnext);
var compilerResults = CompileDLL.RunCompile(codelist.ToArray());
var result = CompileDLL.ConvertError(compilerResults);
if (!string.IsNullOrEmpty(result))
{
throw new Exception(result);
}
//编译新的
dynamicAssembly = compilerResults.CompiledAssembly;
updateFileTime = LastWriteTime;
}
return dynamicAssembly;
}