Created
February 3, 2018 22:36
-
-
Save sebingel/872f434fce85c1f62fc52914e9fc5f6a to your computer and use it in GitHub Desktop.
Creating a Hello World
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://social.msdn.microsoft.com/Forums/en-US/3a720901-fdc5-47dc-8057-e06454525c82/how-to-generate-c-code-at-run-time-using-c-language?forum=csharplanguage | |
// https://benohead.com/three-options-to-dynamically-execute-csharp-code/ | |
// http://stackoverflow.com/questions/875723/how-to-debug-break-in-codedom-compiled-code | |
using System; | |
using System.CodeDom; | |
using System.CodeDom.Compiler; | |
using System.Globalization; | |
using System.IO; | |
using System.Reflection; | |
namespace RuntimeCodeGenerationTest | |
{ | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var codeUnit = new CodeCompileUnit(); | |
var codeNameSpace = new CodeNamespace("DynamicSourceCodeGeneration"); | |
var usingSystemDll = new CodeNamespaceImport("System"); | |
var programClass = new CodeTypeDeclaration("Program") | |
{ | |
IsClass = true, | |
TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed | |
}; | |
codeNameSpace.Imports.Add(usingSystemDll); | |
codeNameSpace.Types.Add(programClass); | |
codeUnit.Namespaces.Add(codeNameSpace); | |
var entryPointMethod = new CodeEntryPointMethod(); | |
var systemConsoleReference = new CodeTypeReferenceExpression("System.Console"); | |
var helloWorldStatement = new CodeMethodInvokeExpression(systemConsoleReference, "WriteLine", | |
new CodePrimitiveExpression("Hello World!")); | |
entryPointMethod.Statements.Add(helloWorldStatement); | |
programClass.Members.Add(entryPointMethod); | |
var codeDomProvider = CodeDomProvider.CreateProvider("CSharp"); | |
var generatorOptions = new CodeGeneratorOptions {BracingStyle = "C"}; | |
using (var sourceWriter = new StreamWriter("HelloWorld.cs")) | |
{ | |
codeDomProvider.GenerateCodeFromCompileUnit(codeUnit, sourceWriter, generatorOptions); | |
} | |
Console.WriteLine(File.ReadAllText("HelloWorld.cs")); | |
Console.WriteLine(); | |
Console.WriteLine("#################################"); | |
Console.WriteLine(); | |
var cp = new CompilerParameters(); | |
cp.ReferencedAssemblies.Add("System.dll"); | |
cp.GenerateExecutable = false; | |
cp.GenerateInMemory = false; //default | |
cp.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true); | |
cp.IncludeDebugInformation = true; | |
var compilerResult = codeDomProvider.CompileAssemblyFromDom(cp, codeUnit); | |
var assembly = compilerResult.CompiledAssembly; | |
var type = assembly.GetType("DynamicSourceCodeGeneration.Program"); | |
var method = type.GetMethod("Main"); | |
var obj = assembly.CreateInstance(type.FullName); | |
method.Invoke(obj, BindingFlags.InvokeMethod, null, null, CultureInfo.CurrentCulture); | |
Console.Write("\nPress any key to exit."); | |
Console.ReadKey(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment