lixiaojun
2024-07-29 72d1856d8097fa55e54e1ed4cc1ae06912f3e57e
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
using System;
using System.Reflection;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Text;
 
namespace CodeProvider
{
    public class CP
    {
        public static dynamic DoMethod(string code,string MethoName,object[] args)
        {
            // 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(code));
 
            if (cr.Errors.HasErrors)
            {
                Console.WriteLine("编译错误:");
                foreach (CompilerError err in cr.Errors)
                {
                    Console.WriteLine(err.ErrorText);
                }
                return null;
            }
            else
            {
                // 通过反射,调用HelloWorld的实例
                Assembly objAssembly = cr.CompiledAssembly;
                if (MethoName== null)MethoName= "OutPut";
                object methodInstanse = objAssembly.CreateInstance($"DynamicCodeGenerate.DynamicMethod");
                MethodInfo objMI = methodInstanse.GetType().GetMethod(MethoName);
 
                return objMI.Invoke(methodInstanse, args);
            }
 
            
        }
 
        static string GenerateCode(string input=null)
        {
            
            
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace DynamicCodeGenerate");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append("    public class DynamicMethod");
            sb.Append(Environment.NewLine);
            sb.Append("    {");
            sb.Append(Environment.NewLine);
 
 
            if (input != null)
            {
                sb.Append(input);
            }
            else
            {
                
                
                sb.Append("        public string OutPut()");
                sb.Append(Environment.NewLine);
                sb.Append("        {");
                sb.Append(Environment.NewLine);
                sb.Append("             return \"Hello world!\";");
                sb.Append(Environment.NewLine);
                sb.Append("        }");
                sb.Append(Environment.NewLine);
                
            }
 
            sb.Append("    }");
            sb.Append(Environment.NewLine);
 
            sb.Append("}");
 
            string code = sb.ToString();
            
 
            return code;
        }
    }
}