コード例 #1
0
            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>));
            }
コード例 #2
0
            private void InitializeSetter(FieldInfo fieldInfo)
            {
                string accessorName = MemberAccessor.GetAccessorName(false, fieldInfo.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, fieldInfo.DeclaringType, true);
                lGenerator.Emit(OpCodes.Ldarg_1);
                MemberAccessor.EmitTypeConversion(lGenerator, fieldInfo.FieldType, false);
                lGenerator.Emit(OpCodes.Stfld, fieldInfo);
                lGenerator.Emit(OpCodes.Ret);
                this.setter = (Action <object, object>)dynamicMethod.CreateDelegate(typeof(Action <object, object>));
            }
コード例 #3
0
            private void InitializeGetter(FieldInfo fieldInfo)
            {
                string accessorName = MemberAccessor.GetAccessorName(true, fieldInfo.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.Emit(OpCodes.Ldarg_0);
                MemberAccessor.EmitTypeConversion(lGenerator, fieldInfo.DeclaringType, true);
                lGenerator.Emit(OpCodes.Ldfld, fieldInfo);
                if (fieldInfo.FieldType.IsValueType)
                {
                    lGenerator.Emit(OpCodes.Box, fieldInfo.FieldType);
                }
                lGenerator.Emit(OpCodes.Ret);
                this.getter = (Func <object, object>)dynamicMethod.CreateDelegate(typeof(Func <object, object>));
            }