Example #1
0
        internal static ISerializer GetSerializer(Type type)
        {
            ISerializer ret;

            if (!serializers.TryGetValue(type, out ret))
            {
                if (type.IsArray)
                {
                    ret = new ArraySerializer(type.GetElementType());
                }
                else
                {
                    if (type.Namespace == "System.Collections.Generic")
                    {
                        switch (type.Name)
                        {
                        case "IDictionary`2": {
                            Type[] types = type.GetGenericArguments();
                            ret = new IDictionary2Serializer(types[0], types[1]);
                            goto end;
                        }

                        case "IEnumerable`1":
                            ret = new IEnumerable1Serializer(type.GetGenericArguments()[0]);
                            goto end;
                        }
                    }
                    foreach (var i in type.GetInterfaces().OrderBy(t => t.Name))                      //orderby zeby slownik jako slownik a nie jako enumeracje key/value
                    {
                        switch (i.Name)
                        {
                        case "IDictionary`2": {
                            Type[] types = i.GetGenericArguments();
                            ret = new IDictionary2Serializer(types[0], types[1]);
                            goto end;
                        }

                        case "IEnumerable`1":
                            ret = new IEnumerable1Serializer(i.GetGenericArguments()[0]);
                            goto end;
                        }
                    }
                    ret = new ClassSerializer(type);
                }
end:
                serializers[type] = ret;
            }
            return(ret);
        }
Example #2
0
 internal static ISerializer GetSerializer(Type type)
 {
     ISerializer ret;
     if (!serializers.TryGetValue(type, out ret)) {
         if (type.IsArray) {
             ret = new ArraySerializer(type.GetElementType());
         } else {
             if (type.Namespace == "System.Collections.Generic") {
                 switch (type.Name) {
                     case "IDictionary`2": {
                             Type[] types = type.GetGenericArguments();
                             ret = new IDictionary2Serializer(types[0], types[1]);
                             goto end;
                         }
                     case "IEnumerable`1":
                         ret = new IEnumerable1Serializer(type.GetGenericArguments()[0]);
                         goto end;
                 }
             }
             foreach (var i in type.GetInterfaces().OrderBy(t => t.Name)) {//orderby zeby slownik jako slownik a nie jako enumeracje key/value
                 switch (i.Name) {
                     case "IDictionary`2": {
                             Type[] types = i.GetGenericArguments();
                             ret = new IDictionary2Serializer(types[0], types[1]);
                             goto end;
                         }
                     case "IEnumerable`1":
                         ret = new IEnumerable1Serializer(i.GetGenericArguments()[0]);
                         goto end;
                 }
             }
             ret = new ClassSerializer(type);
         }
     end:
         serializers[type] = ret;
     }
     return ret;
 }