Advertisement
aetos

Untitled

Oct 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5.  
  6. public class Program
  7. {
  8.     private static void Main(string[] args)
  9.     {
  10.         var asm = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("hoge"), AssemblyBuilderAccess.RunAndSave);
  11.         var mod = asm.DefineDynamicModule("hoge.dll");
  12.         var typ = mod.DefineType("typ");
  13.         var met = typ.DefineMethod("met", MethodAttributes.Public | MethodAttributes.Static);
  14.  
  15.         var exp = Expression.Lambda<Action>(
  16.             Expression.Call(
  17.                 typeof(Console).GetMethod(nameof(Console.WriteLine), new[] { typeof(string) }),
  18.                 Expression.Constant("Hoge")),
  19.             tailCall: true); // tail
  20.  
  21.         exp.CompileToMethod(met);
  22.         typ.CreateType();
  23.         asm.Save("Hoge.dll");
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement