private void InitializeSetter(PropertyInfo propertyInfo, bool requiresSetter) { MethodInfo setMethod = propertyInfo.GetSetMethod(true); if (setMethod == null) { if (requiresSetter) { throw new SerializationException("Property annotated with AmqpMemberAttribute must have a setter."); } return; } string accessorName = MemberAccessor.GetAccessorName(false, propertyInfo.Name); System.Type type = typeof(void); System.Type[] typeArray = new System.Type[] { typeof(object), typeof(object) }; DynamicMethod dynamicMethod = new DynamicMethod(accessorName, type, typeArray, true); ILGenerator lGenerator = dynamicMethod.GetILGenerator(); lGenerator.Emit(OpCodes.Ldarg_0); MemberAccessor.EmitTypeConversion(lGenerator, propertyInfo.DeclaringType, true); lGenerator.Emit(OpCodes.Ldarg_1); MemberAccessor.EmitTypeConversion(lGenerator, propertyInfo.PropertyType, false); MemberAccessor.EmitCall(lGenerator, setMethod); lGenerator.Emit(OpCodes.Ret); this.setter = (Action <object, object>)dynamicMethod.CreateDelegate(typeof(Action <object, object>)); }
private void InitializeGetter(PropertyInfo propertyInfo) { string accessorName = MemberAccessor.GetAccessorName(true, propertyInfo.Name); System.Type type = typeof(object); System.Type[] typeArray = new System.Type[] { typeof(object) }; DynamicMethod dynamicMethod = new DynamicMethod(accessorName, type, typeArray, true); ILGenerator lGenerator = dynamicMethod.GetILGenerator(); lGenerator.DeclareLocal(typeof(object)); lGenerator.Emit(OpCodes.Ldarg_0); MemberAccessor.EmitTypeConversion(lGenerator, propertyInfo.DeclaringType, true); MemberAccessor.EmitCall(lGenerator, propertyInfo.GetGetMethod(true)); if (propertyInfo.PropertyType.IsValueType) { lGenerator.Emit(OpCodes.Box, propertyInfo.PropertyType); } lGenerator.Emit(OpCodes.Ret); this.getter = (Func <object, object>)dynamicMethod.CreateDelegate(typeof(Func <object, object>)); }