编译C#代码

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
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
 
namespace System
{
    public static class CompileCSCAtRuntime
    {
        public static void HelloWorld()
        {
            string code = @"
                using System;
 
                namespace First
                {
                    public class Program
                    {
                        public static void Main()
                        {
                        " +
                            "Console.WriteLine(\"Hello, world!\");"
                          + @"
                        }
                    }
                }
            ";
 
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
 
            // Reference to System.Drawing library
            parameters.ReferencedAssemblies.Add("System.Drawing.dll");
            // True - memory generation, false - external file generation
            parameters.GenerateInMemory = true;
            // True - exe file generation, false - dll file generation
            parameters.GenerateExecutable = true;
 
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
 
            if (results.Errors.HasErrors)
            {
                StringBuilder sb = new StringBuilder();
 
                foreach (CompilerError error in results.Errors)
                {
                    sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
                }
 
                throw new InvalidOperationException(sb.ToString());
            }
 
            Assembly assembly = results.CompiledAssembly;
            Type program = assembly.GetType("First.Program");
            MethodInfo main = program.GetMethod("Main");
 
            main.Invoke(null, null);
        }
 
        public static void TestMeothds()
        {
            MethodInfo function = CreateFunction("x + 2 * y");
            var betterFunction = (Func<double, double, double>)Delegate.CreateDelegate(typeof(Func<double, double, double>), function);
            Func<double, double, double> lambda = (x, y) => x + 2 * y;
 
            DateTime start;
            DateTime stop;
            double result;
            int repetitions = 5000000;
 
            start = DateTime.Now;
            for (int i = 0; i < repetitions; i++)
            {
                result = OriginalFunction(2, 3);
            }
            stop = DateTime.Now;
            Console.WriteLine("Original - time: {0} ms", (stop - start).TotalMilliseconds);
 
            start = DateTime.Now;
            for (int i = 0; i < repetitions; i++)
            {
                result = (double)function.Invoke(null, new object[] { 2, 3 });
            }
            stop = DateTime.Now;
            Console.WriteLine("Reflection - time: {0} ms", (stop - start).TotalMilliseconds);
 
            start = DateTime.Now;
            for (int i = 0; i < repetitions; i++)
            {
                result = betterFunction(2, 3);
            }
            stop = DateTime.Now;
            Console.WriteLine("Delegate - time: {0} ms", (stop - start).TotalMilliseconds);
 
            start = DateTime.Now;
            for (int i = 0; i < repetitions; i++)
            {
                result = lambda(2, 3);
            }
            stop = DateTime.Now;
            Console.WriteLine("Lambda - time: {0} ms", (stop - start).TotalMilliseconds);
        }
 
        public static double OriginalFunction(double x, double y)
        {
            return x + 2 * y;
        }
 
        public static MethodInfo CreateFunction(string function)
        {
            string code = @"
                using System;
             
                namespace UserFunctions
                {               
                    public class BinaryFunction
                    {               
                        public static double Function(double x, double y)
                        {
                            return func_xy;
                        }
                    }
                }
            ";
 
            string finalCode = code.Replace("func_xy", function);
 
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(), finalCode);
 
            Type binaryFunction = results.CompiledAssembly.GetType("UserFunctions.BinaryFunction");
            return binaryFunction.GetMethod("Function");
        }
    }
}

  

posted @   巴别塔  阅读(268)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示