예제 #1
0
        /// <summary>
        /// 创建PropertyInfo的动态方法get
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        private static DynamicPropertyGetHandler CreateGetHandler(PropertyInfo propertyInfo)
        {
            MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true);
            int        argIndex      = 0;

            DynamicMethod dynamicGet   = new DynamicMethod("GP", Type_Object, PropertyGet_P, propertyInfo.DeclaringType, true);
            ILGenerator   getGenerator = dynamicGet.GetILGenerator();

            getGenerator.Emit(OpCodes.Ldarg_0);
            foreach (ParameterInfo parainfo in getMethodInfo.GetParameters())
            {
                getGenerator.Emit(OpCodes.Ldarg_1);
                if (argIndex > 8)
                {
                    getGenerator.Emit(OpCodesFactory.GetLdc_I4(argIndex), argIndex);
                }
                else
                {
                    getGenerator.Emit(OpCodesFactory.GetLdc_I4(argIndex));
                }
                getGenerator.Emit(OpCodes.Ldelem_Ref);
                OpCodesFactory.UnboxIfNeeded(getGenerator, parainfo.ParameterType);
                argIndex++;
            }
            getGenerator.Emit(OpCodes.Callvirt, getMethodInfo);
            OpCodesFactory.BoxIfNeeded(getGenerator, getMethodInfo.ReturnType);
            getGenerator.Emit(OpCodes.Ret);

            return((DynamicPropertyGetHandler)dynamicGet.CreateDelegate(Type_DPGH));
        }
예제 #2
0
        /// <summary>
        /// 创建Field的动态方法get
        /// </summary>
        /// <param name="fieldInfo"></param>
        /// <returns></returns>
        private static DynamicFieldGetHandler CreateGetHandler(FieldInfo fieldInfo)
        {
            DynamicMethod dynamicGet   = new DynamicMethod("GF", Type_Object, FieldGet_P, fieldInfo.DeclaringType, true);
            ILGenerator   getGenerator = dynamicGet.GetILGenerator();

            getGenerator.Emit(OpCodes.Ldarg_0);
            getGenerator.Emit(OpCodes.Ldfld, fieldInfo);
            OpCodesFactory.BoxIfNeeded(getGenerator, fieldInfo.FieldType);
            getGenerator.Emit(OpCodes.Ret);

            return((DynamicFieldGetHandler)dynamicGet.CreateDelegate(Type_DFGH));
        }
예제 #3
0
        private static DynamicMethodHandler CreatMethodDelegate(MethodInfo genericMethodInfo, Type[] genericParameterTypes)
        {
            MethodInfo makeGenericMethodInfo;

            if (genericParameterTypes != null && genericParameterTypes.Length > 0)
            {
                makeGenericMethodInfo = genericMethodInfo.MakeGenericMethod(genericParameterTypes);
            }
            else
            {
                makeGenericMethodInfo = genericMethodInfo;
            }

            DynamicMethod dynamicMethod = new DynamicMethod("DM", Type_Object, PropertyGet_P, makeGenericMethodInfo.DeclaringType.Module);
            ILGenerator   il            = dynamicMethod.GetILGenerator();

            ParameterInfo[] ps         = makeGenericMethodInfo.GetParameters();
            Type[]          paramTypes = new Type[ps.Length];
            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (ps[i].ParameterType.IsByRef)
                {
                    paramTypes[i] = ps[i].ParameterType.GetElementType();
                }
                else
                {
                    paramTypes[i] = ps[i].ParameterType;
                }
            }
            LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];

            for (int i = 0; i < paramTypes.Length; i++)
            {
                locals[i] = il.DeclareLocal(paramTypes[i], true);
            }
            for (int i = 0; i < paramTypes.Length; i++)
            {
                il.Emit(OpCodes.Ldarg_1);
                OpCodesFactory.EmitFastInt(il, i);
                il.Emit(OpCodes.Ldelem_Ref);
                OpCodesFactory.EmitCastToReference(il, paramTypes[i]);
                il.Emit(OpCodes.Stloc, locals[i]);
            }
            if (!makeGenericMethodInfo.IsStatic)
            {
                il.Emit(OpCodes.Ldarg_0);
            }
            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (ps[i].ParameterType.IsByRef)
                {
                    il.Emit(OpCodes.Ldloca_S, locals[i]);
                }
                else
                {
                    il.Emit(OpCodes.Ldloc, locals[i]);
                }
            }
            if (makeGenericMethodInfo.IsStatic)
            {
                il.EmitCall(OpCodes.Call, makeGenericMethodInfo, null);
            }
            else
            {
                il.EmitCall(OpCodes.Callvirt, makeGenericMethodInfo, null);
            }
            if (makeGenericMethodInfo.ReturnType == typeof(void))
            {
                il.Emit(OpCodes.Ldnull);
            }
            else
            {
                OpCodesFactory.BoxIfNeeded(il, makeGenericMethodInfo.ReturnType);
            }

            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (ps[i].ParameterType.IsByRef)
                {
                    il.Emit(OpCodes.Ldarg_1);
                    OpCodesFactory.EmitFastInt(il, i);
                    il.Emit(OpCodes.Ldloc, locals[i]);
                    if (locals[i].LocalType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, locals[i].LocalType);
                    }
                    il.Emit(OpCodes.Stelem_Ref);
                }
            }

            il.Emit(OpCodes.Ret);
            return((DynamicMethodHandler)dynamicMethod.CreateDelegate(Type_DM));
        }