private static ReflectionEmitUtils.SetterMethod GenerateSetter(Type memberType, Type parentType, Action <ILGenerator> codeGenerator)
        {
            Type typeFromHandle = typeof(void);

            Type[] argumentTypes = new Type[]
            {
                typeof(object),
                typeof(object)
            };
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.SetterMethod>(typeFromHandle, argumentTypes, parentType, delegate(ILGenerator il)
            {
                il.Emit(OpCodes.Ldarg_0);
                if (parentType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox, parentType);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, parentType);
                }
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Unbox_Any, memberType);
                codeGenerator(il);
            }));
        }
 public static ReflectionEmitUtils.GetterMethod GenerateGetter(FieldInfo fieldInfo)
 {
     return(ReflectionEmitUtils.GenerateGetter(fieldInfo.FieldType, fieldInfo.DeclaringType, delegate(ILGenerator il)
     {
         il.Emit(OpCodes.Ldfld, fieldInfo);
     }));
 }
        public static ReflectionEmitUtils.ConstructorMethod GenerateConstructor(Type type)
        {
            ConstructorInfo constructor = null;

            if (!type.IsValueType)
            {
                constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                if (constructor == null)
                {
                    return(null);
                }
            }
            Type typeFromHandle = typeof(object);

            Type[] emptyTypes = Type.EmptyTypes;
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.ConstructorMethod>(typeFromHandle, emptyTypes, type, delegate(ILGenerator il)
            {
                if (type.IsValueType)
                {
                    il.DeclareLocal(type);
                    il.Emit(OpCodes.Ldloca_S, 0);
                    il.Emit(OpCodes.Initobj, type);
                    il.Emit(OpCodes.Ldloc_0);
                    il.Emit(OpCodes.Box, type);
                    return;
                }
                il.Emit(OpCodes.Newobj, constructor);
            }));
        }
 public static ReflectionEmitUtils.GetterMethod GenerateGetter(PropertyInfo propertyInfo)
 {
     return(ReflectionEmitUtils.GenerateGetter(propertyInfo.PropertyType, propertyInfo.DeclaringType, delegate(ILGenerator il)
     {
         MethodInfo getMethod = propertyInfo.GetGetMethod(true);
         il.Emit(OpCodes.Callvirt, getMethod);
     }));
 }
 protected override void Initialize(IntermediateSerializer serializer, PropertyInfo propertyInfo)
 {
     this.valueGetter = ReflectionEmitUtils.GenerateGetter(propertyInfo);
     if (propertyInfo.CanWrite)
     {
         this.valueSetter = ReflectionEmitUtils.GenerateSetter(propertyInfo);
     }
     this.InitializeCommon(serializer, propertyInfo, propertyInfo.PropertyType);
 }
 protected override void Initialize(IntermediateSerializer serializer, FieldInfo fieldInfo, bool canWrite)
 {
     this.valueGetter = ReflectionEmitUtils.GenerateGetter(fieldInfo);
     if (canWrite)
     {
         this.valueSetter = ReflectionEmitUtils.GenerateSetter(fieldInfo);
     }
     this.InitializeCommon(serializer, fieldInfo, fieldInfo.FieldType);
 }
示例#7
0
        internal CollectionHelper(IntermediateSerializer serializer, Type type)
        {
            this.targetType = type;
            Type type2 = CollectionUtils.CollectionElementType(type, false);

            if (type2 == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.NotACollectionType, new object[]
                {
                    type
                }));
            }
            this.contentSerializer = serializer.GetTypeSerializer(type2);
            Type type3 = typeof(ICollection <>).MakeGenericType(new Type[]
            {
                type2
            });

            this.countPropertyGetter = ReflectionEmitUtils.GenerateGetter(type3.GetProperty("Count"));
            this.addToCollection     = ReflectionEmitUtils.GenerateAddToCollection(type3, type2);
        }
        public static ReflectionEmitUtils.SetterMethod GenerateAddToCollection(Type collectionType, Type elementType)
        {
            Type typeFromHandle = typeof(void);

            Type[] argumentTypes = new Type[]
            {
                typeof(object),
                typeof(object)
            };
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.SetterMethod>(typeFromHandle, argumentTypes, elementType, delegate(ILGenerator il)
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, collectionType);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Unbox_Any, elementType);
                Type[] types = new Type[]
                {
                    elementType
                };
                MethodInfo method = collectionType.GetMethod("Add", types);
                il.Emit(OpCodes.Callvirt, method);
            }));
        }
 public ReflectiveSerializer(Type targetType) : base(targetType)
 {
     this.instanceConstructor = ReflectionEmitUtils.GenerateConstructor(targetType);
 }