|
|
浮子屋商店もよろしく。 |
|
C#のコードをメモリ内でコンパイルして実行
0
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
| | using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeCompiler cc = cscp.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.CompilerOptions="/r:System.dll;System.Windows.Forms.dll";
string txtsrc=
"using System;"+
"using System.Windows.Forms;"+
"namespace Testnamespace{"+
"public class TestClass{"+
"public void Test(string s){"+
"MeesageBox.Show(s);"+
"}"+
"}"+
"}";
CompilerResults cr = cc.CompileAssemblyFromSource(param, txtsrc);
foreach(CompilerError ce in cr.Errors){
}
Assembly asm;
try{
asm = cr.CompiledAssembly;
}catch(Exception ex){
}
Type type = asm.GetType("Testnamespace.TestClass");
object o=asm.CreateInstance("Testnamespace.TestClass");
MethodInfo mi = type.GetMethod("Test");
mi.Invoke(o, new object[]{"Hello,world"});
|
|