-
Notifications
You must be signed in to change notification settings - Fork 896
/
MerhabaDunyaReflection.cs
40 lines (31 loc) · 1.38 KB
/
MerhabaDunyaReflection.cs
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
38
39
40
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
var name = "MerhabaDunya.exe";
var assemblyname = new AssemblyName(name);
var assemblybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyname, AssemblyBuilderAccess.RunAndSave);
var modulebuilder = assemblybuilder.DefineDynamicModule(name);
var program = modulebuilder.DefineType("Program", TypeAttributes.Public);
var mainmethod = program.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(string[]) });
var ilgenerator = mainmethod.GetILGenerator();
ilgenerator.Emit(OpCodes.Ldstr, "Merhaba Dunya!");
ilgenerator.Emit(OpCodes.Call, (typeof(Console)).GetMethod("WriteLine", new Type[] { typeof(string) }));
ilgenerator.Emit(OpCodes.Call, (typeof(Console)).GetMethod("ReadKey", new Type[0]));
ilgenerator.Emit(OpCodes.Pop);
ilgenerator.Emit(OpCodes.Ret);
program.CreateType();
assemblybuilder.SetEntryPoint(((Type)program).GetMethod("Main"));
File.Delete(name);
assemblybuilder.Save(name);
Process.Start(name);
}
}
}