MSIL Hello World

AssemblyName _assemblyName = new AssemblyName();
_assemblyName.Name = "helloWorld";
AssemblyBuilder _assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(_assemblyName, AssemblyBuilderAccess.RunAndSave);

ModuleBuilder _modBuilder = _assemblyBuilder.DefineDynamicModule(_assemblyName.Name+ ".exe");

TypeBuilder _typeBuilder = _modBuilder.DefineType("Foo");


MethodBuilder _methodBuilder = _typeBuilder.DefineMethod("Main", MethodAttributes.Static, typeof(void), System.Type.EmptyTypes);
ILGenerator _il = _methodBuilder.GetILGenerator();

_il.EmitWriteLine("hello World ");
_il.Emit(OpCodes.Ret);

_typeBuilder.CreateType();
_modBuilder.CreateGlobalFunctions();
_assemblyBuilder.SetEntryPoint(_methodBuilder, PEFileKinds.ConsoleApplication);
_assemblyBuilder.Save(_assemblyName.Name + ".exe");
_il = null;

Comments

Popular posts from this blog

ionic2 cordova build android - Unable resolve gradle 2.2.3

OpenCover code coverage for .Net Core

Using Custom DLL with IronPython / Scripts