private SetPropertyValueDelegate CreateSetPropertyValueMethod(PropertyInfo pi) { MethodInfo methodInfo = pi.GetSetMethod(true); if (methodInfo == null) { return(null); } DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(void), new Type[] { typeof(object), typeof(object) }); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal.LocalIndex); il.Emit(OpCodes.Ldloc, thisLocal); il.Emit(OpCodes.Ldarg_1); ILUtil.UnboxIfNeeded(il, methodInfo.GetParameters()[0].ParameterType); il.Emit(OpCodes.Call, methodInfo); il.Emit(OpCodes.Ret); return((SetPropertyValueDelegate)method.CreateDelegate(typeof(SetPropertyValueDelegate))); }
private GetPropertyValueDelegate CreateGetPropertyValueMethod(PropertyInfo pi) { MethodInfo methodInfo = pi.GetGetMethod(); if (methodInfo == null) { return(null); } DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object), new Type[] { typeof(object) }); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal.LocalIndex); il.Emit(OpCodes.Ldloc, thisLocal); il.EmitCall(OpCodes.Callvirt, methodInfo, null); ILUtil.BoxIfNeeded(il, pi.PropertyType); il.Emit(OpCodes.Ret); return((GetPropertyValueDelegate)method.CreateDelegate(typeof(GetPropertyValueDelegate))); }
private SetFieldValueDelegate CreateSetFieldValueMethod(FieldInfo field) { DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, null, new Type[] { typeof(object), typeof(object) }); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal.LocalIndex); Type valueType = field.FieldType; // load 'this' il.Emit(OpCodes.Ldloc, thisLocal); il.Emit(OpCodes.Ldarg_1); ILUtil.PrepareFieldForSet(il, valueType); il.Emit(OpCodes.Stfld, field); il.Emit(OpCodes.Ret); return((SetFieldValueDelegate)method.CreateDelegate(typeof(SetFieldValueDelegate))); }
private GetFieldValueDelegate CreateGetFieldValueMethod(FieldInfo field) { DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object), new Type[] { typeof(object) }); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); LocalBuilder dataLocal = il.DeclareLocal(typeof(object)); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal); ConstructorInfo ci = ReflectionUtil.GetDefaultConstructor(typeof(object)); il.Emit(OpCodes.Newobj, ci); il.Emit(OpCodes.Stloc, dataLocal); // get the value il.Emit(OpCodes.Ldloc, thisLocal); il.Emit(OpCodes.Ldfld, field); ILUtil.BoxIfNeeded(il, field.FieldType); il.Emit(OpCodes.Stloc, dataLocal); il.Emit(OpCodes.Ldloc, dataLocal); il.Emit(OpCodes.Ret); return((GetFieldValueDelegate)method.CreateDelegate(typeof(GetFieldValueDelegate))); }
private SetFieldValuesDelegate CreateSetFieldValuesMethod(FieldInfo[] fields) { Type[] methodArguments = new Type[] { typeof(object), typeof(object[]) }; DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, null, methodArguments); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal.LocalIndex); for (int i = 0; i < fields.Length; i++) { Type valueType = fields[i].FieldType; // load 'this' il.Emit(OpCodes.Ldloc, thisLocal); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldelem_Ref); ILUtil.PrepareFieldForSet(il, valueType); il.Emit(OpCodes.Stfld, fields[i]); } il.Emit(OpCodes.Ret); return((SetFieldValuesDelegate)method.CreateDelegate(typeof(SetFieldValuesDelegate))); }
private GetFieldValuesDelegate CreateGetFieldValuesMethod(FieldInfo[] fields) { Type[] methodArguments = new Type[] { typeof(object) }; DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object[]), methodArguments); ILGenerator il = method.GetILGenerator(); LocalBuilder thisLocal = il.DeclareLocal(m_refinedType); LocalBuilder dataLocal = il.DeclareLocal(typeof(object[])); il.Emit(OpCodes.Ldarg_0); ILUtil.EmitCastToReference(il, m_reflectedType); il.Emit(OpCodes.Stloc, thisLocal); il.Emit(OpCodes.Ldc_I4, fields.Length); il.Emit(OpCodes.Newarr, typeof(object)); il.Emit(OpCodes.Stloc, dataLocal); for (int i = 0; i < fields.Length; i++) { FieldInfo field = fields[i]; il.Emit(OpCodes.Ldloc, dataLocal); il.Emit(OpCodes.Ldc_I4, i); // get the value il.Emit(OpCodes.Ldloc, thisLocal); il.Emit(OpCodes.Ldfld, field); ILUtil.BoxIfNeeded(il, field.FieldType); il.Emit(OpCodes.Stelem_Ref); } il.Emit(OpCodes.Ldloc, dataLocal.LocalIndex); il.Emit(OpCodes.Ret); return((GetFieldValuesDelegate)method.CreateDelegate(typeof(GetFieldValuesDelegate))); }