Example #1
0
        private void GenerateForVirtualMethods()
        {
            //Constructor
            DefineConstructorsForSubClass();

            //SetInterceptors method
            DefineSetInterceptorsMethod(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Final);

            //Methods
            foreach (var methodInfo in TypeToIntercept.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (!methodInfo.IsSpecialName && methodInfo.IsOverridable() && Interceptors.Contains(methodInfo))
                {
                    var attributes = GetMethodAttributes(methodInfo);
                    if (null != attributes)
                    {
                        DefineInterceptableMethod(methodInfo, attributes.Value);
                    }
                }
            }

            //Properties
            foreach (var property in TypeToIntercept.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                PropertyBuilder propertyBuilder = null;

                //Getter
                var getMethod = property.GetMethod;
                if (getMethod != null && getMethod.IsOverridable() && Interceptors.Contains(getMethod))
                {
                    var attributes = GetMethodAttributes(getMethod);
                    if (null != attributes)
                    {
                        var parameterTypes = property.GetIndexParameters().Select(it => it.ParameterType).ToArray();
                        propertyBuilder = TypeBuilder.DefineProperty(property.Name, property.Attributes, property.PropertyType, parameterTypes);
                        var methodBuilder = DefineInterceptableMethod(getMethod, attributes.Value);
                        propertyBuilder.SetGetMethod(methodBuilder);
                    }
                }

                //Setter
                var setMethod = property.SetMethod;
                if (setMethod != null && getMethod.IsOverridable() && Interceptors.Contains(setMethod))
                {
                    var attributes = GetMethodAttributes(setMethod);
                    if (null != attributes)
                    {
                        var parameterTypes = property.GetIndexParameters().Select(it => it.ParameterType).ToArray();
                        propertyBuilder = propertyBuilder ?? TypeBuilder.DefineProperty(property.Name, property.Attributes, property.PropertyType, parameterTypes);
                        var methodBuilder = DefineInterceptableMethod(setMethod, attributes.Value);
                        propertyBuilder.SetSetMethod(methodBuilder);
                    }
                }
            }
        }
Example #2
0
        private void GenerateForInterface()
        {
            var attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final;

            DefineConstructorForImplementationClass();

            //Methods
            foreach (var methodInfo in TypeToIntercept.GetMethods().Where(it => !it.IsSpecialName))
            {
                if (Interceptors.Contains(methodInfo))
                {
                    DefineInterceptableMethod(methodInfo, attributes);
                }
                else
                {
                    DefineNonInterceptableMethod(methodInfo, attributes);
                }
            }

            //Properties
            foreach (var property in TypeToIntercept.GetProperties())
            {
                var parameterTypes  = property.GetIndexParameters().Select(it => it.ParameterType).ToArray();
                var propertyBuilder = TypeBuilder.DefineProperty(property.Name, property.Attributes, property.PropertyType, parameterTypes);
                var getMethod       = property.GetMethod;
                if (null != getMethod)
                {
                    var getMethodBuilder = Interceptors.IsInterceptable(getMethod)
                        ? DefineInterceptableMethod(getMethod, attributes)
                        : DefineNonInterceptableMethod(getMethod, attributes);
                    propertyBuilder.SetGetMethod(getMethodBuilder);
                }
                var setMethod = property.SetMethod;
                if (null != setMethod)
                {
                    var setMethodBuilder = Interceptors.IsInterceptable(setMethod)
                        ? DefineInterceptableMethod(setMethod, attributes)
                        : DefineNonInterceptableMethod(setMethod, attributes);
                    propertyBuilder.SetGetMethod(setMethodBuilder);
                }
            }

            //Events
            foreach (var eventInfo in TypeToIntercept.GetEvents())
            {
                var eventBuilder = TypeBuilder.DefineEvent(eventInfo.Name, eventInfo.Attributes, eventInfo.EventHandlerType);
                eventBuilder.SetAddOnMethod(DefineNonInterceptableMethod(eventInfo.AddMethod, attributes));
                eventBuilder.SetRemoveOnMethod(DefineNonInterceptableMethod(eventInfo.RemoveMethod, attributes));
            }
        }
        /// <summary>
        /// Defines the constructors for class inheriting the specified type.
        /// </summary>
        /// <returns>The <see cref="ConstructorBuilder"/> array representing the generated constructors.</returns>
        protected virtual ConstructorBuilder[] DefineConstructorsForSubClass()
        {
            var constructors        = TypeToIntercept.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
            var constructorBuilders = new ConstructorBuilder[constructors.Length];

            for (int index1 = 0; index1 < constructors.Length; index1++)
            {
                var constructor        = constructors[index1];
                var parameterTypes     = constructor.GetParameters().Select(it => it.ParameterType).ToArray();
                var constructorBuilder = constructorBuilders[index1] = TypeBuilder.DefineConstructor(constructor.Attributes, CallingConventions.HasThis, parameterTypes);
                var il = constructorBuilder.GetILGenerator();

                il.Emit(OpCodes.Ldarg_0);
                for (int index2 = 0; index2 < parameterTypes.Length; index2++)
                {
                    il.EmitLoadArgument(index2);
                }
                il.Emit(OpCodes.Call, constructor);
                il.Emit(OpCodes.Ret);
            }
            return(constructorBuilders);
        }