zoukankan      html  css  js  c++  java
  • 使用反射创建动态模块

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Reflection;
    using System.Reflection.Emit;

    namespace Emit
    {
        public class CModuleBuilder
        {
            public static void Demo(System.Windows.Controls.TextBlock outputBlock)
            {
                // 创建应用程序域.
                AppDomain myCurrentDomain = AppDomain.CurrentDomain;
                AssemblyName myAssemblyName = new AssemblyName("TempAssembly");

                // 创建动态程序集
                AssemblyBuilder myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
                               (myAssemblyName, AssemblyBuilderAccess.Run);

                // 动态模块.
                ModuleBuilder myModuleBuilder = myAssemblyBuilder.
                                                DefineDynamicModule("TempModule");

                // 定义类.
                TypeBuilder myTypeBuilder = myModuleBuilder.DefineType
                                                 ("TempClass", TypeAttributes.Public);

                // 添加字段.
                FieldBuilder greetingField = myTypeBuilder.DefineField("Greeting",
                                                       typeof(String), FieldAttributes.Public);
                Type[] myMethodArgs = { typeof(String) };

                // 动态类的方法.
                MethodBuilder myMethod = myTypeBuilder.DefineMethod("MyMethod",
                   MethodAttributes.Public, CallingConventions.Standard, typeof(String), myMethodArgs);

                // 方法的属性.
                MethodInfo stringConcat = typeof(String).GetMethod("Concat",
                   BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder,
                   new Type[] { typeof(String), typeof(String) },
                   null);

                ILGenerator methodIL = myMethod.GetILGenerator();
                methodIL.EmitWriteLine("In the method...");
                methodIL.Emit(OpCodes.Ldarg_0);
                methodIL.Emit(OpCodes.Ldarg_1);
                methodIL.Emit(OpCodes.Stfld, greetingField);
                methodIL.Emit(OpCodes.Ldstr, "Hello, ");
                methodIL.Emit(OpCodes.Ldarg_1);
                methodIL.Emit(OpCodes.Call, stringConcat);
                methodIL.Emit(OpCodes.Ret);

                Type myType = myTypeBuilder.CreateType();
                MethodInfo myMethodInfo =
                                        myType.GetMethod("MyMethod");

                // Get the token used to identify the method within this module.
                MethodToken myMethodToken =
                                  myModuleBuilder.GetMethodToken(myMethodInfo);
                outputBlock.Text += String.Format(
                   "Token used to identify the method of 'myType' within the module is {0:x}\n",
                   myMethodToken.Token);

                object[] args = { "World!" };
                object myObject = Activator.CreateInstance(myType);
                outputBlock.Text += "Return value from MyMethod(\"World!\"): " +
                   myMethodInfo.Invoke(myObject, args);
            }

        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    PHP学习—了解篇2
    PHP学习—了解篇
    德国山寨史
    Network client/server
    Linux (二) vi
    I2C 总线协议
    Verilog (二) multiplexer and decoder
    有限状态机HDL模板
    "德意志之歌"的历史
    【转】java与.net比较学习系列(3) 基本数据类型和类型转换
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519247.html
Copyright © 2011-2022 走看看