ILGenerator.Emit動態 MSIL編程(三)之動態代理

來源:互聯網
上載者:User

標籤: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);        }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.