/// <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)); }
/// <summary> /// 创建构造函数 /// </summary> /// <param name="constructorInfo"></param> /// <returns></returns> private static DynamicConstructorInfoHandler CreateDynamicConstructorInfoHandler(ConstructorInfo constructorInfo) { int argIndex = 0; DynamicMethod dynamicMethod = new DynamicMethod("D", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, Type_Object, FieldGet_P, constructorInfo.DeclaringType, true); ILGenerator generator = dynamicMethod.GetILGenerator(); foreach (ParameterInfo parainfo in constructorInfo.GetParameters()) { generator.Emit(OpCodes.Ldarg_0); if (argIndex > 8) { generator.Emit(OpCodesFactory.GetLdc_I4(argIndex), argIndex); } else { generator.Emit(OpCodesFactory.GetLdc_I4(argIndex)); } generator.Emit(OpCodes.Ldelem_Ref); OpCodesFactory.UnboxIfNeeded(generator, parainfo.ParameterType); argIndex++; } generator.Emit(OpCodes.Newobj, constructorInfo); generator.Emit(OpCodes.Ret); return((DynamicConstructorInfoHandler)dynamicMethod.CreateDelegate(Type_DCIH)); }
/// <summary> /// 创建Field的动态方法set /// </summary> /// <param name="fieldInfo"></param> /// <returns></returns> private static DynamicFieldSetHandler CreateSetHandler(FieldInfo fieldInfo) { DynamicMethod dynamicSet = new DynamicMethod("SF", Type_Void, FieldSet_P, fieldInfo.DeclaringType, true); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); OpCodesFactory.UnboxIfNeeded(setGenerator, fieldInfo.FieldType); setGenerator.Emit(OpCodes.Stfld, fieldInfo); setGenerator.Emit(OpCodes.Ret); return((DynamicFieldSetHandler)dynamicSet.CreateDelegate(Type_DFSH)); }
/// <summary> /// 创建PropertyInfo的动态方法set /// </summary> /// <param name="propertyInfo"></param> /// <returns></returns> private static DynamicPropertySetHandler CreateSetHandler(PropertyInfo propertyInfo) { MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true); int argCount = setMethodInfo.GetParameters().Length; int argIndex = 0; DynamicMethod dynamicSet = new DynamicMethod("SP", Type_Void, PropertySet_P, propertyInfo.DeclaringType, true); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); foreach (ParameterInfo parainfo in setMethodInfo.GetParameters()) { if (argIndex + 1 >= argCount) { break; } setGenerator.Emit(OpCodes.Ldarg_2); if (argIndex > 8) { setGenerator.Emit(OpCodesFactory.GetLdc_I4(argIndex), argIndex); } else { setGenerator.Emit(OpCodesFactory.GetLdc_I4(argIndex)); } setGenerator.Emit(OpCodes.Ldelem_Ref); OpCodesFactory.UnboxIfNeeded(setGenerator, parainfo.ParameterType); argIndex++; } setGenerator.Emit(OpCodes.Ldarg_1); OpCodesFactory.UnboxIfNeeded(setGenerator, setMethodInfo.GetParameters()[argIndex].ParameterType); setGenerator.Emit(OpCodes.Call, setMethodInfo); setGenerator.Emit(OpCodes.Ret); return((DynamicPropertySetHandler)dynamicSet.CreateDelegate(Type_DPSH)); }