From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
HMI.Core/Util/DllHelper.cs | 197 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 197 insertions(+), 0 deletions(-)
diff --git a/HMI.Core/Util/DllHelper.cs b/HMI.Core/Util/DllHelper.cs
new file mode 100644
index 0000000..6f01c9b
--- /dev/null
+++ b/HMI.Core/Util/DllHelper.cs
@@ -0,0 +1,197 @@
+锘縰sing 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
+ {
+ // 閫氳繃鍙嶅皠锛岃皟鐢℉elloWorld鐨勫疄渚�
+ 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;
+ }
+ }
+}
--
Gitblit v1.9.1