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
{
///
/// DLL文件操作类
///
public class DllHelper
{
///
/// 获取当前DLL所处的路径
///
/// Dll路径
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;
}
///
/// 导入DLL(要导入的DLL要放在程序的根目录中)
///
/// 要导入的DLL名称
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);
}
}
///
/// 导入DLL
///
/// 要导入的DLL文件路径
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);
}
}
///
/// 生成无参数的构造函数的实例
///
/// 命名空间
/// 包含命名控件的类名
/// 生成的对应类的实例
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);
}
}
///
/// 动态编译和执行语句
///
/// 要编译执行的语句
/// 执行的结果
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;
}
///
/// 生成编译语句
///
/// 需要编译的语句
/// 编译执行后的结果
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;
}
///
///
///
///
///
///
public static T DeepCopy(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;
}
}
}