CompilerInvoke
$ make mmm
cat makefile
TARGET=compileInvoke.exe
SRCS=$(TARGET:%.exe=%.cs)
.PHONY: all clean mmm test
.SUFFIXES: .cs .exe
all: $(TARGET)
$(TARGET): $(SRCS)
.cs.exe:
csc $<
test: $(TARGET)
./$<
clean:
@-rm *~ *.obj $(TARGET) 1>/dev/null 2>&1
mmm:
cat makefile
cat $(SRCS)
cat compileInvoke.cs
// compileinvoke.cs
// csc compileInvoke.cs
using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
public class CompileInvoke {
static string cs = @"
public class CSHello {
public static void Main() {
System.Console.WriteLine(""Hello C# World!"");
}
}";
public static void Main() {
// Console.WriteLine(cs);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CShar
p");
String[] referenceAssemblies = { "System.dll" };
String exeFile = "aaa.exe";
CompilerParameters cp = new CompilerParameters(referenceAssembli
es,exeFile, false);
cp.GenerateInMemory = true;
cp.GenerateExecutable = true;
CompilerResults cr = provider.CompileAssemblyFromSource(cp, cs);
Assembly asm = cr.CompiledAssembly;
Type type = asm.GetType("CSHello");
MethodInfo mi = type.GetMethod("Main");
mi.Invoke(null, null); // Hello C# World!
}
}