Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SOURCE: http://stackoverflow.com/questions/4181668/execute-c-sharp-code-at-runtime-from-code-file
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Net;
- using Microsoft.CSharp;
- using System.CodeDom.Compiler;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string source =
- @"
- namespace Foo
- {
- public class Bar
- {
- public void SayHello()
- {
- System.Console.WriteLine(""Hello World"");
- }
- }
- }
- ";
- Dictionary<string, string> providerOptions = new Dictionary<string, string>
- {
- {"CompilerVersion", "v3.5"}
- };
- CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
- CompilerParameters compilerParams = new CompilerParameters
- {GenerateInMemory = true,
- GenerateExecutable = false};
- CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
- if (results.Errors.Count != 0)
- throw new Exception("Mission failed!");
- object o = results.CompiledAssembly.CreateInstance("Foo.Bar");
- MethodInfo mi = o.GetType().GetMethod("SayHello");
- mi.Invoke(o, null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement