Exemplo n.º 1
0
        public void Generate(MethodInfo newMethod)
        {
            if (newMethod.IsGenericMethod)
            {
                newMethod = newMethod.GetGenericMethodDefinition();
            }
            FieldInfo field = typeof(DynamicWrapperBase).GetField("UnderlyingObject", BindingFlags.Instance | BindingFlags.NonPublic);

            ParameterInfo[] parameters     = newMethod.GetParameters();
            Type[]          parameterTypes = (from parameter in parameters
                                              select parameter.ParameterType).ToArray <Type>();
            MethodBuilder methodBuilder = this._wrapperBuilder.DefineMethod(newMethod.Name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual, newMethod.ReturnType, parameterTypes);

            if (newMethod.IsGenericMethod)
            {
                methodBuilder.DefineGenericParameters((from arg in newMethod.GetGenericArguments()
                                                       select arg.Name).ToArray <string>());
            }
            ILGenerator iLGenerator = methodBuilder.GetILGenerator();

            WrapperMethodBuilder.LoadUnderlyingObject(iLGenerator, field);
            WrapperMethodBuilder.PushParameters(parameters, iLGenerator);
            this.ExecuteMethod(newMethod, parameterTypes, iLGenerator);
            WrapperMethodBuilder.Return(iLGenerator);
        }
Exemplo n.º 2
0
        private static Type GenerateWrapperType(Type interfaceType, Type underlyingType)
        {
            TypeBuilder typeBuilder = ModuleBuilder.DefineType("{0}_{1}_Wrapper".FormatWith(CultureInfo.InvariantCulture, interfaceType.Name, underlyingType.Name), TypeAttributes.Sealed, typeof(DynamicWrapperBase), new Type[1] {
                interfaceType
            });
            WrapperMethodBuilder wrapperMethodBuilder = new WrapperMethodBuilder(underlyingType, typeBuilder);

            foreach (MethodInfo item in interfaceType.AllMethods())
            {
                wrapperMethodBuilder.Generate(item);
            }
            return(typeBuilder.CreateType());
        }
Exemplo n.º 3
0
        private static Type GenerateWrapperType(Type interfaceType, Type underlyingType)
        {
            TypeBuilder proxyBuilder = DynamicWrapper.ModuleBuilder.DefineType(StringUtils.FormatWith("{0}_{1}_Wrapper", (IFormatProvider)CultureInfo.InvariantCulture, (object)interfaceType.Name, (object)underlyingType.Name), TypeAttributes.Sealed, typeof(DynamicWrapperBase), new Type[1]
            {
                interfaceType
            });
            WrapperMethodBuilder wrapperMethodBuilder = new WrapperMethodBuilder(underlyingType, proxyBuilder);

            foreach (MethodInfo newMethod in TypeExtensions.GetAllMethods(interfaceType))
            {
                wrapperMethodBuilder.Generate(newMethod);
            }
            return(proxyBuilder.CreateType());
        }
Exemplo n.º 4
0
        public void Generate(MethodInfo newMethod)
        {
            if (newMethod.IsGenericMethod)
            {
                newMethod = newMethod.GetGenericMethodDefinition();
            }
            FieldInfo field = typeof(DynamicWrapperBase).GetField("UnderlyingObject", BindingFlags.Instance | BindingFlags.NonPublic);

            ParameterInfo[] parameters     = newMethod.GetParameters();
            Type[]          parameterTypes = Enumerable.ToArray <Type>(Enumerable.Select <ParameterInfo, Type>((IEnumerable <ParameterInfo>)parameters, (Func <ParameterInfo, Type>)(parameter => parameter.ParameterType)));
            MethodBuilder   methodBuilder  = this._wrapperBuilder.DefineMethod(newMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, newMethod.ReturnType, parameterTypes);

            if (newMethod.IsGenericMethod)
            {
                methodBuilder.DefineGenericParameters(Enumerable.ToArray <string>(Enumerable.Select <Type, string>((IEnumerable <Type>)newMethod.GetGenericArguments(), (Func <Type, string>)(arg => arg.Name))));
            }
            ILGenerator ilGenerator = methodBuilder.GetILGenerator();

            WrapperMethodBuilder.LoadUnderlyingObject(ilGenerator, field);
            WrapperMethodBuilder.PushParameters((ICollection <ParameterInfo>)parameters, ilGenerator);
            this.ExecuteMethod((MethodBase)newMethod, parameterTypes, ilGenerator);
            WrapperMethodBuilder.Return(ilGenerator);
        }