-
Notifications
You must be signed in to change notification settings - Fork 1
/
n.cs
109 lines (92 loc) · 3.14 KB
/
n.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
namespace DynaCode
{
class Program
{
static string[] code = {
"using System;"+
"namespace DynaCore"+
"{"+
" public class DynaCore"+
" {"+
" static public int Main()"+
" {"+
" {0}"+
" }"+
" }"+
"}"};
static void Main(string[] args)
{
Console.WriteLine(code[0]);
string op = Console.ReadLine();
string ret = "return " + op;
Console.WriteLine(ret);
Console.WriteLine("a{0}", ret);
code[0] = string.Format(code[0], ret);
Console.WriteLine(code);
Console.ReadKey();
CompileAndRun(code);
Console.ReadKey();
}
static void CompileAndRun(string[] code)
{
CompilerParameters CompilerParams = new CompilerParameters();
string outputDirectory = Directory.GetCurrentDirectory();
CompilerParams.GenerateInMemory = true;
CompilerParams.TreatWarningsAsErrors = false;
CompilerParams.GenerateExecutable = false;
CompilerParams.CompilerOptions = "/optimize";
string[] references = { "System.dll" };
CompilerParams.ReferencedAssemblies.AddRange(references);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);
if (compile.Errors.HasErrors)
{
string text = "Compile error: ";
foreach (CompilerError ce in compile.Errors)
{
text += "rn" + ce.ToString();
}
throw new Exception(text);
}
//ExpoloreAssembly(compile.CompiledAssembly);
Module module = compile.CompiledAssembly.GetModules()[0];
Type mt = null;
MethodInfo methInfo = null;
if (module != null)
{
mt = module.GetType("DynaCore.DynaCore");
}
if (mt != null)
{
methInfo = mt.GetMethod("Main");
}
if (methInfo != null)
{
Console.WriteLine(methInfo.Invoke(null, new object[] {}));
}
}
static void ExpoloreAssembly(Assembly assembly)
{
Console.WriteLine("Modules in the assembly:");
foreach (Module m in assembly.GetModules())
{
Console.WriteLine("{0}", m);
foreach (Type t in m.GetTypes())
{
Console.WriteLine("t{0}", t.Name);
foreach (MethodInfo mi in t.GetMethods())
{
Console.WriteLine("tt{0}", mi.Name);
}
}
}
}
}
}