using System;
|
using System.CodeDom.Compiler;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Microsoft.CSharp;
|
|
namespace HMI.Core.Util
|
{
|
/// <summary>
|
/// DLL文件操作类
|
/// </summary>
|
public class DllHelper
|
{
|
/// <summary>
|
/// 获取当前DLL所处的路径
|
/// </summary>
|
/// <returns>Dll路径</returns>
|
public static string GetAssemblyPath()
|
{
|
string CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
|
CodeBase = CodeBase.Substring(8, CodeBase.Length - 8); // 8是 file:// 的长度
|
string[] arrSection = CodeBase.Split(new char[] { '/' });
|
string FolderPath = string.Empty;
|
for (int i = 0; i < arrSection.Length - 1; i++)
|
{
|
FolderPath += arrSection[i] + "\\";
|
}
|
|
return FolderPath;
|
}
|
|
/// <summary>
|
/// 导入DLL(要导入的DLL要放在程序的根目录中)
|
/// </summary>
|
/// <param name="DLLName">要导入的DLL名称</param>
|
public static void ImportDLL(string DllPath)
|
{
|
try
|
{
|
//string DllPath = GetAssemblyPath() + DLLName;
|
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
|
foreach (Assembly dll in assemblyArray)
|
{
|
if (dll.Location == DllPath)
|
{
|
return;
|
}
|
}
|
|
Assembly.LoadFrom(DllPath);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 导入DLL
|
/// </summary>
|
/// <param name="DllPath">要导入的DLL文件路径</param>
|
public static void ImportDLL2(string DllPath)
|
{
|
try
|
{
|
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
|
foreach (Assembly dll in assemblyArray)
|
{
|
if (!dll.FullName.Contains("Microsoft.GeneratedCode"))
|
{
|
if (dll.Location == DllPath)
|
{
|
return;
|
}
|
}
|
}
|
|
Assembly.LoadFile(DllPath);
|
}
|
catch (Exception ex)
|
{
|
throw new Exception(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 生成无参数的构造函数的实例
|
/// </summary>
|
/// <param name="NameSpaceStr">命名空间</param>
|
/// <param name="ClassStr">包含命名控件的类名</param>
|
/// <returns>生成的对应类的实例</returns>
|
public static object CreateInstance(string NameSpaceStr, string ClassStr)
|
{
|
try
|
{
|
Type DllType = Assembly.Load(NameSpaceStr).GetType(ClassStr); // 生成类型
|
object Instance = Activator.CreateInstance(DllType); // 生成实例
|
return Instance;
|
}
|
catch (Exception ex)
|
{
|
throw new Exception(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 动态编译和执行语句
|
/// </summary>
|
/// <param name="Expression">要编译执行的语句</param>
|
/// <returns>执行的结果</returns>
|
public static object GetValue(string Expression)
|
{
|
object Rtn = null;
|
// 1.CSharpCodePrivoder
|
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
|
// 2.ICodeComplier
|
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
|
// 3.CompilerParameters
|
CompilerParameters objCompilerParameters = new CompilerParameters();
|
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
|
objCompilerParameters.GenerateExecutable = false;
|
objCompilerParameters.GenerateInMemory = true;
|
// 4.CompilerResults
|
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode(Expression));
|
if (cr.Errors.HasErrors)
|
{
|
throw new Exception("编译错误:" + cr.Errors[0].ErrorText);
|
}
|
else
|
{
|
// 通过反射,调用HelloWorld的实例
|
Assembly objAssembly = cr.CompiledAssembly;
|
object Obj = objAssembly.CreateInstance("CommonLibs.Eval");
|
MethodInfo objMI = Obj.GetType().GetMethod("GetValue");
|
Rtn = objMI.Invoke(Obj, null);
|
}
|
return Rtn;
|
}
|
|
/// <summary>
|
/// 生成编译语句
|
/// </summary>
|
/// <param name="Expression">需要编译的语句</param>
|
/// <returns>编译执行后的结果</returns>
|
private static string GenerateCode(string Expression)
|
{
|
StringBuilder sb = new StringBuilder();
|
sb.Append("using System;");
|
sb.Append(Environment.NewLine);
|
sb.Append("namespace CommonLibs");
|
sb.Append(Environment.NewLine);
|
sb.Append("{");
|
sb.Append(Environment.NewLine);
|
sb.Append(" public class Eval");
|
sb.Append(Environment.NewLine);
|
sb.Append(" {");
|
sb.Append(Environment.NewLine);
|
sb.Append(" public object GetValue()");
|
sb.Append(Environment.NewLine);
|
sb.Append(" {");
|
sb.Append(Environment.NewLine);
|
sb.Append(" return " + Expression + " ;");
|
sb.Append(Environment.NewLine);
|
sb.Append(" }");
|
sb.Append(Environment.NewLine);
|
sb.Append(" }");
|
sb.Append(Environment.NewLine);
|
sb.Append("}");
|
string code = sb.ToString();
|
return code;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static T DeepCopy<T>(T obj)
|
{
|
//如果是字符串或值类型则直接返回
|
if (obj is string || obj.GetType().IsValueType) return obj;
|
|
object retval = Activator.CreateInstance(obj.GetType());
|
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
|
foreach (FieldInfo field in fields)
|
{
|
try { field.SetValue(retval, DeepCopy(field.GetValue(obj))); }
|
catch { }
|
}
|
return (T)retval;
|
}
|
}
|
}
|