首页 > 其他 > 详细

生成DLL

时间:2015-07-16 16:21:17      阅读:150      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

namespace EmitDemo
{
    public class DLLCreator
    {
        public static void Main(string[] arsg)
        {
            var asmName = new AssemblyName("Test");
            var asmBuider = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
            var mdlbldr = asmBuider.DefineDynamicModule("Hello", "Hello.exe");
            var typebldr = mdlbldr.DefineType("Hello", TypeAttributes.Public);
            var methodbldr = typebldr.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Static, null, null);
            var il = methodbldr.GetILGenerator();
            il.Emit(OpCodes.Ldstr, "Hello ,world");
            il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
            il.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
            il.Emit(OpCodes.Pop);//读入的值会被推送至evaluation stack,而本方法是没有返回值的,因此,需要将栈上的值抛弃    
            il.Emit(OpCodes.Ret);
            var t = typebldr.CreateType();
            asmBuider.SetEntryPoint(t.GetMethod("SayHello"));
            asmBuider.Save("Hello.exe");
        }
    }
}

 

生成DLL

原文:http://www.cnblogs.com/ziranquliu/p/4651173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!