Exemplo n.º 1
0
 public GenericClassSerializer GetFieldSerializer(FieldInfo field)
 {
     GenericClassSerializer ret = null;
     Type fieldType = field.FieldType;
     if (fieldType.IsGenericType &&
         ((fieldType.GetGenericTypeDefinition() == typeof(IList<>)) || (fieldType.GetGenericTypeDefinition() == typeof(List<>))))
     {
         POxOSerializerClassPair pair = new POxOSerializerClassPair();
         recirsiveFindSerializer(fieldType, pair);
         ret = pair.getSerializer();
     }
     else if (fieldType.IsGenericType &&
         ((fieldType.GetGenericTypeDefinition() == typeof(IDictionary<,>)) || (fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>))))
     {
         POxOSerializerClassPair pair = new POxOSerializerClassPair();
         recirsiveFindSerializer(fieldType, pair);
         ret = pair.getSerializer();
     }
     else
     {
         ret = GetTypeSerializer(fieldType);
     }
     return ret;
 }
Exemplo n.º 2
0
        private void recirsiveFindSerializer(Type genericType, POxOSerializerClassPair pair)
        {
            if (!genericType.IsGenericType)
            {
                pair.setGenericClass(genericType);
                pair.setSerializer(GetTypeSerializer(genericType));
            }
            else
            {
                Type[] genericTypes = genericType.GetGenericArguments();
                POxOSerializerClassPair[] nestedPairs = new POxOSerializerClassPair[genericTypes.Length];
                for (int i = 0; i < genericTypes.Length; i++)
                {
                    Type actualType = genericTypes[i];
                    POxOSerializerClassPair nestedPair = new POxOSerializerClassPair();
                    recirsiveFindSerializer(actualType, nestedPair);
                    nestedPairs[i] = nestedPair;
                }
                if ((genericType.GetGenericTypeDefinition() == typeof(IList<>)) || (genericType.GetGenericTypeDefinition() == typeof(List<>)))
                {
                    pair.setGenericClass(genericType);
                    pair.setSerializer(new ListSerializer(nestedPairs[0].getGenericClass(), nestedPairs[0].getSerializer()));
                }
                else
                {
                    if ((genericType.GetGenericTypeDefinition() == typeof(IDictionary<,>)) || (genericType.GetGenericTypeDefinition() == typeof(Dictionary<,>)))
                    {
                        pair.setGenericClass(genericType);
                        pair.setSerializer(new MapSerializer(nestedPairs[0].getGenericClass(),
                                        nestedPairs[1].getGenericClass(), nestedPairs[0].getSerializer(),
                                        nestedPairs[1].getSerializer()));
                    }
                }
            }

            //if (genericType instanceof GenericArrayType) {
              //  Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
              //  if (componentType instanceof Type) {
              //    pair.setGenericClass((Type) componentType);
              //    pair.setSerializer(new ListSerializer((Type) componentType, getTypeSerializer((Type) componentType)));
              //    return;
              //  } else {
              //    POxOSerializerClassPair nestedPair = new POxOSerializerClassPair();
              //    recirsiveFindSerializer(componentType, nestedPair);
              //    pair.setGenericClass(nestedPair.getGenericClass());
              //    pair.setSerializer(new ListSerializer(nestedPair.getGenericClass(),
              //        nestedPair.getSerializer()));
              //    return;
              //  }
              //}
              //if (genericType instanceof ParameterizedType) {
              //  Type[] actualTypes = ((ParameterizedType) genericType).getActualTypeArguments();
              //  Type genericClass = (Type) ((ParameterizedType) genericType).getRawType();
              //  if (Map.class.isAssignableFrom(genericClass)) {
              //    POxOSerializerClassPair[] serializers = new POxOSerializerClassPair[actualTypes.length];
              //    for (int i = 0, n = actualTypes.length; i < n; i++) {
              //      Type actualType = actualTypes[i];

              //      if (actualType instanceof Type) {
              //        POxOSerializerClassPair nestedPair = new POxOSerializerClassPair();
              //        nestedPair.setGenericClass((Type) actualType);
              //        nestedPair.setSerializer(getTypeSerializer((Type) actualType));
              //        serializers[i] = nestedPair;
              //      } else if (actualType instanceof ParameterizedType) {
              //        POxOSerializerClassPair nestedPair = new POxOSerializerClassPair();
              //        recirsiveFindSerializer(actualType, nestedPair);
              //        serializers[i] = nestedPair;
              //      } else
              //        continue;
              //    }
              //    pair.setGenericClass((Type) ((ParameterizedType) genericType).getRawType());
              //    pair.setSerializer(new MapSerializer(serializers[0].getGenericClass(),
              //        serializers[1].getGenericClass(), serializers[0].getSerializer(),
              //        serializers[1].getSerializer()));
              //  } else if (List.class.isAssignableFrom(genericClass)) {
              //    for (int i = 0, n = actualTypes.length; i < n; i++) {
              //      Type actualType = actualTypes[i];

              //      if (actualType instanceof Type) {
              //        pair.setGenericClass((Type) ((ParameterizedType) genericType).getRawType());
              //        pair.setSerializer(new ListSerializer((Type) actualType, getTypeSerializer((Type) actualType)));
              //      } else if (actualType instanceof ParameterizedType) {
              //        POxOSerializerClassPair nestedPair = new POxOSerializerClassPair();
              //        recirsiveFindSerializer(actualType, nestedPair);
              //        pair.setGenericClass((Type) ((ParameterizedType) genericType).getRawType());
              //        pair.setSerializer(new ListSerializer(nestedPair.getGenericClass(),
              //            nestedPair.getSerializer()));
              //      } else
              //        continue;
              //    }
              //  }
              //}
        }