Selaa lähdekoodia

Condense serializer registration

habeebweeb 3 vuotta sitten
vanhempi
commit
67afe297f8

+ 13 - 19
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Serialization/Serialization.cs

@@ -7,39 +7,33 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 {
     public static class Serialization
     {
-        private static readonly Dictionary<Type, ISerializer> serializers = new();
+        private static readonly Dictionary<Type, ISerializer> Serializers;
 
-        private static readonly Dictionary<Type, ISimpleSerializer> simpleSerializers = new();
+        private static readonly Dictionary<Type, ISimpleSerializer> SimpleSerializers;
 
         static Serialization()
         {
             var types = (from t in typeof(MeidoPhotoStudio).Assembly.GetTypes()
                 let baseType = t.BaseType
-                where !t.IsAbstract && !t.IsInterface && baseType != null && baseType.IsGenericType
+                where !t.IsAbstract && !t.IsInterface && baseType?.IsGenericType == true
                 select new { type = t, baseType }).ToArray();
 
-            var concreteSerializers = from t in types
-                where t.baseType.GetGenericTypeDefinition() == typeof(Serializer<>)
-                select new { t.type, arg = t.baseType.GetGenericArguments().First() };
+            Serializers = types.Where(t => t.baseType.GetGenericTypeDefinition() == typeof(Serializer<>))
+                .Select(t => new {t.type, arg = t.baseType.GetGenericArguments()[0]})
+                .ToDictionary(x => x.arg, x => (ISerializer)Activator.CreateInstance(x.type));
 
-            foreach (var serializer in concreteSerializers)
-                serializers[serializer.arg] = (ISerializer) Activator.CreateInstance(serializer.type);
-
-            var concreteSimpleSerializers = from t in types
-                where t.baseType.GetGenericTypeDefinition() == typeof(SimpleSerializer<>)
-                select new { t.type, arg = t.baseType.GetGenericArguments().First() };
-
-            foreach (var serializer in concreteSimpleSerializers)
-                simpleSerializers[serializer.arg] = (ISimpleSerializer) Activator.CreateInstance(serializer.type);
+            SimpleSerializers = types.Where(t => t.baseType.GetGenericTypeDefinition() == typeof(SimpleSerializer<>))
+                .Select(t => new {t.type, arg = t.baseType.GetGenericArguments()[0]})
+                .ToDictionary(x => x.arg, x => (ISimpleSerializer)Activator.CreateInstance(x.type));
         }
 
-        public static Serializer<T> Get<T>() => serializers[typeof(T)] as Serializer<T>;
+        public static Serializer<T> Get<T>() => Serializers[typeof(T)] as Serializer<T>;
 
-        public static ISerializer Get(Type type) => serializers[type];
+        public static ISerializer Get(Type type) => Serializers[type];
 
-        public static SimpleSerializer<T> GetSimple<T>() => simpleSerializers[typeof(T)] as SimpleSerializer<T>;
+        public static SimpleSerializer<T> GetSimple<T>() => SimpleSerializers[typeof(T)] as SimpleSerializer<T>;
 
-        public static ISimpleSerializer GetSimple(Type type) => simpleSerializers[type];
+        public static ISimpleSerializer GetSimple(Type type) => SimpleSerializers[type];
 
         public static short ReadVersion(this BinaryReader reader) => reader.ReadInt16();