예제 #1
0
        /// <summary>
        /// Bind a generated proxy method to an existing target method
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="proxyMethodInfo"></param>
        /// <param name="typeBuilder"></param>
        /// <param name="ldindOpCodeTypeMap"></param>
        /// <param name="stindOpCodeTypeMap"></param>
        /// <returns></returns>
        public static MethodBuilder BindMethod(DynamicMethodBinder binder, MethodInfo proxyMethodInfo, TypeBuilder typeBuilder, Dictionary <Type, OpCode> ldindOpCodeTypeMap, Dictionary <Type, OpCode> stindOpCodeTypeMap)
        {
            var paramInfos = proxyMethodInfo.GetParameters();
            int nofParams  = paramInfos.Length;

            Type[] parameterTypes = new Type[nofParams];
            for (int i = 0; i < nofParams; i++)
            {
                parameterTypes[i] = paramInfos[i].ParameterType;
            }
            Type returnType    = proxyMethodInfo.ReturnType;
            var  methodBuilder = typeBuilder.DefineMethod(proxyMethodInfo.Name, MethodAttributes.Public | MethodAttributes.Virtual, returnType, parameterTypes);

            var mIL = methodBuilder.GetILGenerator();
            // TODO: Inject call to binder
            var binderType       = binder.GetType();
            var binderInvokeInfo = binderType.GetMethod(DynamicMethodBinder.InvokeMethodName, BindingFlags.Instance | BindingFlags.Public);

            GenerateILBinding(binderInvokeInfo, proxyMethodInfo, mIL, parameterTypes, returnType, ldindOpCodeTypeMap, stindOpCodeTypeMap);

            return(methodBuilder);
        }
예제 #2
0
        internal static TInterface BuildEmpty <TInterface>(DynamicMethodBinder binder) where TInterface : class
        {
            var paramType = binder.GetType().GetTypeInfo().GetConstructors().First().GetParameters().First().ParameterType;

            return(ProxyFactory.CreateEmptyProxy <TInterface>(binder, binder.GetType(), paramType, binder.Target));
        }