alielie
2025-11-21 3a471ae651b228231bebb3a2d438baa1d4ac0261
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
        }
    }
}