/// <summary> /// Initializes dynamic method /// </summary> /// <param name="methodInfo"></param> private static DynamicMethodDelegate CreateMethodProxy(MethodInfo methodInfo) { var method = new DynamicMethod(methodInfo.Name + "_Proxy", typeof(object), new Type[] { typeof(object), typeof(object[]) }, methodInfo.DeclaringType); var wrapper = new ILGeneratorWrapper(method.GetILGenerator()); var parameters = methodInfo.GetParameters(); var parameterTypes = new Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { parameterTypes[i] = parameters[i].ParameterType; } wrapper.DeclareLocals(parameterTypes); for (int i = 0; i < parameterTypes.Length; i++) { wrapper.EmitLoadArg(1); wrapper.EmitLoadCons(i); wrapper.Emit(OpCodes.Ldelem_Ref); wrapper.EmitCast(parameterTypes[i]); wrapper.EmitStoreLocal(i); } if (!methodInfo.IsStatic) { wrapper.Emit(OpCodes.Ldarg_0); } for (int i = 0; i < parameterTypes.Length; i++) { wrapper.EmitLoadLoc(i); } wrapper.EmitCall(methodInfo); if (methodInfo.ReturnType == typeof(void)) { wrapper.Emit(OpCodes.Ldnull); } else if (methodInfo.ReturnType.IsValueType) { wrapper.EmitBox(methodInfo.ReturnType); } wrapper.Emit(OpCodes.Ret); return(method.CreateDelegate(typeof(DynamicMethodDelegate)) as DynamicMethodDelegate); }
/// <summary> /// Creates the dynamic proxy method which calls given <see cref="ConstructorInfo"/> /// </summary> /// <param name="methodInfo"></param> private static DynamicMethodDelegate CreateProxyMethod(ConstructorInfo constructor) { var method = new DynamicMethod(constructor.DeclaringType.Name + "_ctor_Proxy", typeof(object), new Type[] { typeof(object[]) }, constructor.DeclaringType); var wrapper = new ILGeneratorWrapper(method.GetILGenerator()); var parameters = constructor.GetParameters(); var parameterTypes = new Type[parameters.Length + 1]; parameterTypes[0] = constructor.DeclaringType; for (int i = 0; i < parameters.Length; i++) { parameterTypes[i + 1] = parameters[i].ParameterType; } wrapper.DeclareLocals(parameterTypes); for (int i = 1; i < parameterTypes.Length; i++) { wrapper.EmitLoadArg(0); wrapper.EmitLoadCons(i - 1); wrapper.Emit(OpCodes.Ldelem_Ref); wrapper.EmitCast(parameterTypes[i]); wrapper.EmitStoreLocal(i); } for (int i = 1; i < parameterTypes.Length; i++) { wrapper.EmitLoadLoc(i); } wrapper.Emit_NewObj(constructor); wrapper.EmitStoreLocal(0); wrapper.EmitLoadLoc(0); wrapper.Emit(OpCodes.Ret); return(method.CreateDelegate(typeof(DynamicMethodDelegate)) as DynamicMethodDelegate); }
/// <summary> /// Creates set method proxy for the given <see cref="FieldInfo"/> /// </summary> /// <param name="info">The field info</param> private static SetFieldDelegate CreateSetMethodProxy(FieldInfo info) { var method = new DynamicMethod(info.Name + "_Set_Proxy", typeof(void), new[] { typeof(object), typeof(object) }, info.DeclaringType); var wrapper = new ILGeneratorWrapper(method.GetILGenerator()); wrapper.DeclareLocals(new Type[] { info.FieldType }); wrapper.EmitLoadArg(0); wrapper.EmitLoadArg(1); if (info.FieldType.IsValueType) { wrapper.EmitUnbox_Any(info.FieldType); } wrapper.EmitStoreField(info); wrapper.Emit(OpCodes.Ret); return(method.CreateDelegate(typeof(SetFieldDelegate)) as SetFieldDelegate); }
/// <summary> /// Creates get method proxy for the given <see cref="FieldInfo"/> /// </summary> /// <param name="fieldInfo">The field info</param> private static GetFieldDelegate CreateGetMethodProxy(FieldInfo fieldInfo) { var method = new DynamicMethod(fieldInfo.Name + "_Get_Proxy", typeof(object), new[] { typeof(object) }, fieldInfo.DeclaringType); var wrapper = new ILGeneratorWrapper(method.GetILGenerator()); wrapper.DeclareLocals(new Type[] { fieldInfo.FieldType }); wrapper.EmitLoadArg(0); wrapper.EmitLoadField(fieldInfo); wrapper.EmitStoreLocal(0); wrapper.EmitLoadLoc(0); if (fieldInfo.FieldType.IsValueType) { wrapper.EmitBox(fieldInfo.FieldType); } wrapper.Emit(OpCodes.Ret); return(method.CreateDelegate(typeof(GetFieldDelegate)) as GetFieldDelegate); }