Emit 网上转载的.留着以后用

通过Emit动态创建类(C#)
2009-08-16 22:02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace EmitDemo
{
    public interface IHello
    {
        void SayHello(string toWhom);
    }

    class Program
    {
        static void Main(string[] args)
        {
            AssemblyName asmName = new AssemblyName();

            asmName.Name = "HelloWorld";

            AssemblyBuilder asmBuilder =
                Thread.GetDomain().DefineDynamicAssembly
            (asmName, AssemblyBuilderAccess.RunAndSave);

            ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule
                            ("HelloWorld");

            TypeBuilder typeBuilder = modBuilder.DefineType(
                                    "Hello",
                                    TypeAttributes.Public,
                                    typeof(object),
                                    new Type[] { typeof(IHello) });

            MethodBuilder methodBuilder = typeBuilder.DefineMethod("SayHello",
                         MethodAttributes.Private | MethodAttributes.Virtual,
                         typeof(void),
                         new Type[] { typeof(string) });

            typeBuilder.DefineMethodOverride(methodBuilder,
                    typeof(IHello).GetMethod("SayHello"));

            ILGenerator il = methodBuilder.GetILGenerator();

            // string.Format("Hello, {0} World!", toWhom)
            //
            il.Emit(OpCodes.Ldstr, "Hello, {0} World!");
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Call, typeof(string).GetMethod
        ("Format", new Type[] { typeof(string), typeof(object) }));

            // Console.WriteLine("Hello, World!");
            //
            il.Emit(OpCodes.Call, typeof(Console).GetMethod
            ("WriteLine", new Type[] { typeof(string) }));
            il.Emit(OpCodes.Ret);

            Type type = typeBuilder.CreateType();

            IHello hello = (IHello)Activator.CreateInstance(type);

            hello.SayHello("Emit");

            Console.Read();
        }
    }
}

REF;http://www.codeproject.com/KB/msil/emithelper.aspx

Fortunately, there is an alternative way. Business Logic Toolkit for .NET provides a helper class, EmitHelper, which can make your life a little bit easy. It contains typed wrapper methods for almost all Emit commands and allows writing of source code that looks similar to MSIL.

The following examples show how to use the EmitHelper class with C#, VB.NET, and C++/CLI.

幸运的是,有人给我们封装了这个类,Business Logic Toolkit for .NET

下面是使用的例子

using System;

using BLToolkit.Reflection;
using BLToolkit.Reflection.Emit;

namespace EmitHelperDemo
{
    public interface IHello
    {
        void SayHello(string toWhom);
    }

    class Program
    {
        static void Main(string[] args)
        {
            EmitHelper emit = new AssemblyBuilderHelper("HelloWorld.dll")
                .DefineType ("Hello", typeof(object), typeof(IHello))
                .DefineMethod(typeof(IHello).GetMethod("SayHello"))
                .Emitter;

            emit
                // string.Format("Hello, {0} World!", toWhom)
                //
                .ldstr   ("Hello, {0} World!")
                .ldarg_1
                .call    (typeof(string), "Format", typeof(string),
                                            typeof(object))

                // Console.WriteLine("Hello, World!");
                //
                .call    (typeof(Console), "WriteLine", typeof(string))
                .ret()
                ;

            Type   type = emit.Method.Type.Create();

            IHello hello = (IHello)TypeAccessor.CreateInstance(type);

            hello.SayHello("C#");
        }
    }
}

转载于:https://www.cnblogs.com/IT-TI/archive/2013/03/30/2990192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值