码迷,mamicode.com
首页 > 其他好文 > 详细

ILGenerator.Emit动态 MSIL编程(三)之动态代理

时间:2014-06-26 13:08:59      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   color   

  using System;
    using System.Linq;
    using System.Reflection;
    using System.Reflection.Emit;

    public sealed class DynamicProxy
    {
        private static readonly string AssemblyName = "DynamicProxy", 
                                       ModuleName = "DynamicProxy", 
                                       TypeName = "DynamicProxy";
        private AssemblyBuilder CreateDynamicAssembly<T>() where T : class
        {
            return AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(AssemblyName + typeof(T).Name),
                AssemblyBuilderAccess.Run);
        }
        private ModuleBuilder CreateDynamicModule<T>() where T : class
        {
            return CreateDynamicAssembly<T>().DefineDynamicModule(ModuleName + typeof(T).Name);
        }
        /// <summary>
        /// 创建动态代理
        /// </summary>
        public T CreateDynamicType<T>() where T : class,new()
        {
            TypeBuilder typeBuilder = CreateDynamicModule<T>().DefineType(TypeName + typeof(T).Name, TypeAttributes.Public | 
                TypeAttributes.Class, typeof(T));
            TypeActuator<T>(typeBuilder);
            return Activator.CreateInstance(typeBuilder.CreateType()) as T;
        }
        private void BuildCtorMethod(Type classType, FieldBuilder fieldBuilder, TypeBuilder typeBuilder)
        {
            var structureBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,null);
            var ilCtor = structureBuilder.GetILGenerator();
            ilCtor.Emit(OpCodes.Ldarg_0);
            ilCtor.Emit(OpCodes.Newobj, classType.GetConstructor(Type.EmptyTypes));
            ilCtor.Emit(OpCodes.Stfld, fieldBuilder);
            ilCtor.Emit(OpCodes.Ret);
        }
        private void BuildMethod(ILGenerator il, MethodInfo methodInfo, Type[] ParameterTypes)
        {
            il.Emit(OpCodes.Ldarg_0);
            for (int i = 0; i < ParameterTypes.Length; i++)
                il.Emit(OpCodes.Ldarg_S, (short)(i + 1));
            il.Emit(OpCodes.Call, methodInfo);
            il.Emit(OpCodes.Ret);
        }
        private void TypeActuator<T>(TypeBuilder builder) where T : class
        {
            FieldBuilder fieldBuilder = builder.DefineField("_DynamicProxyActuator", typeof(T), FieldAttributes.Private);
            BuildCtorMethod(typeof(T), fieldBuilder, builder);
            MethodInfo[] info = GetMethodInfo(typeof(T));
            foreach (MethodInfo methodInfo in info)
            {
                if (!methodInfo.IsVirtual && !methodInfo.IsAbstract) continue;
                if (methodInfo.Name == "ToString") continue;
                if (methodInfo.Name == "GetHashCode") continue;
                if (methodInfo.Name == "Equals") continue;
                var ParameterTypes = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();
                MethodBuilder methodBuilder = CreateMethod(builder, methodInfo.Name, MethodAttributes.Public | MethodAttributes.Virtual,
                    CallingConventions.Standard, methodInfo.ReturnType, ParameterTypes);
                var ilMethod = methodBuilder.GetILGenerator();
                BuildMethod(ilMethod, methodInfo, ParameterTypes);
            }
        }
        private MethodBuilder CreateMethod(TypeBuilder typeBuilder, string name, MethodAttributes attrs, CallingConventions callingConventions, 
            Type type, Type[] parameterTypes)
        {
            return typeBuilder.DefineMethod(name, attrs, callingConventions, type, parameterTypes);
        }
        private MethodInfo[] GetMethodInfo(Type type)
        {
            return type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
        }
    }

 

ILGenerator.Emit动态 MSIL编程(三)之动态代理,布布扣,bubuko.com

ILGenerator.Emit动态 MSIL编程(三)之动态代理

标签:des   style   class   blog   code   color   

原文地址:http://www.cnblogs.com/yuming1983/p/3809240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!