zoukankan      html  css  js  c++  java
  • c# emit 动态实现接口

    using System;
    using System.Linq;
    using System.Reflection;
    using System.Reflection.Emit;
    
    
    namespace TestEmit{
    
       public interface IPerson{
            void SayHello();
        
        }
    
       class Person2 {
           public void SayHello() {
    
               Console.WriteLine("i am person2");
           }
       }
    
        class Test2
        {
            static void Main(string[] args)
            {
                IPerson p = (IPerson)CreateType();
                p.SayHello();
    
     
                Console.ReadLine();
            }
    
    
    
            public static object CreateType()
            {
                AssemblyName assemblyName = new AssemblyName("assemblyName");
                AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
                ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
                TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
                //*添加所实现的接口
                typeBuilder.AddInterfaceImplementation(typeof(IPerson));
    
              //实现方法
                MethodBuilder mbIM = typeBuilder.DefineMethod("SayHello",
               MethodAttributes.Public | MethodAttributes.HideBySig |
                   MethodAttributes.NewSlot | MethodAttributes.Virtual |
                   MethodAttributes.Final,
               null,
               Type.EmptyTypes);
           
                ILGenerator il = mbIM.GetILGenerator();
                il.Emit(OpCodes.Ldstr, "The SayHello implementation of IPerson");
                il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine",
                    new Type[] { typeof(string) }));
                il.Emit(OpCodes.Ret);
                typeBuilder.DefineMethodOverride(mbIM, typeof(IPerson).GetMethod("SayHello"));
     //添加方法
                //MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public, null, null);
         
    
                //ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
                //ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
                //ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
                //ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
                ////没有返回值所有加OpCodes.Pop
                //ilOfSayHello.Emit(OpCodes.Pop);
                //ilOfSayHello.Emit(OpCodes.Ret);
                Type personType = typeBuilder.CreateType();
                assemblyBuilder.Save("Person.dll");
                object obj = Activator.CreateInstance(personType);
                MethodInfo methodInfo = personType.GetMethod("SayHello");
                
                methodInfo.Invoke(obj, null);
                return Activator.CreateInstance(personType);
            }
    
        }
    
    
     
    }
  • 相关阅读:
    飞机游戏
    nodejs制作爬虫程序
    关于解析字符串
    引用nodejs的url模块实现url路由功能
    appium定位学习
    appium移动端自动化测试的一些感想
    appium的工作原理
    appium desktop 定位弹出框时报错
    APPium连接真机输入框中输入的内容与代码中不一致
    appium 链接真机后,运行代码,但是APP并没有启动
  • 原文地址:https://www.cnblogs.com/tiancai/p/8283182.html
Copyright © 2011-2022 走看看