From e336efc149a83b4f5b4e5612b0a4e27dbd10faf4 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 16 Sep 2025 22:29:52 -0400 Subject: [PATCH] Do the same for serializers --- .../Serializers/BaseBinarySerializer.cs | 116 +----------------- 1 file changed, 6 insertions(+), 110 deletions(-) diff --git a/SabreTools.Serialization/Serializers/BaseBinarySerializer.cs b/SabreTools.Serialization/Serializers/BaseBinarySerializer.cs index 89d62dbb..84c55fc0 100644 --- a/SabreTools.Serialization/Serializers/BaseBinarySerializer.cs +++ b/SabreTools.Serialization/Serializers/BaseBinarySerializer.cs @@ -1,6 +1,4 @@ -using System; using System.IO; -using System.Reflection; using SabreTools.Serialization.Interfaces; namespace SabreTools.Serialization.Serializers @@ -9,7 +7,10 @@ namespace SabreTools.Serialization.Serializers /// Base class for all binary serializers /// /// Type of the model to serialize - /// These methods assume there is a concrete implementation of the serializer for the model available + /// + /// This class allows all inheriting types to only implement + /// and still implicitly implement and + /// public abstract class BaseBinarySerializer : IByteSerializer, IFileSerializer, @@ -20,7 +21,7 @@ namespace SabreTools.Serialization.Serializers /// public virtual byte[]? SerializeArray(TModel? obj) { - using var stream = SerializeStream(obj); + using var stream = Serialize(obj); if (stream == null) return null; @@ -39,7 +40,7 @@ namespace SabreTools.Serialization.Serializers if (string.IsNullOrEmpty(path)) return false; - using var stream = SerializeStream(obj); + using var stream = Serialize(obj); if (stream == null) return false; @@ -56,110 +57,5 @@ namespace SabreTools.Serialization.Serializers public abstract Stream? Serialize(TModel? obj); #endregion - - #region Static Implementations - - /// - public static byte[]? SerializeBytes(TModel? obj) - { - var serializer = GetType>(); - if (serializer == null) - return default; - - return serializer.SerializeArray(obj); - } - - /// - public static bool SerializeFile(TModel? obj, string? path) - { - var serializer = GetType>(); - if (serializer == null) - return default; - - return serializer.Serialize(obj, path); - } - - /// - public static Stream? SerializeStream(TModel? obj) - { - var serializer = GetType>(); - if (serializer == null) - return default; - - return serializer.Serialize(obj); - } - - #endregion - - #region Helpers - - /// - /// Get a constructed instance of a type, if possible - /// - /// Serializer type to construct - /// Serializer of the requested type, null on error - private static TSerializer? GetType() - { - // If the serializer type is invalid - string? serializerName = typeof(TSerializer)?.Name; - if (serializerName == null) - return default; - - // If the serializer has no generic arguments - var genericArgs = typeof(TSerializer).GetGenericArguments(); - if (genericArgs == null || genericArgs.Length == 0) - return default; - - // Loop through all loaded assemblies - Type modelType = genericArgs[0]; - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - // If the assembly is invalid - if (assembly == null) - return default; - - // If not all types can be loaded, use the ones that could be - Type?[] assemblyTypes = []; - try - { - assemblyTypes = assembly.GetTypes(); - } - catch (ReflectionTypeLoadException rtle) - { - assemblyTypes = rtle.Types ?? []; - } - - // Loop through all types - foreach (Type? type in assemblyTypes) - { - // If the type is invalid - if (type == null) - continue; - - // If the type isn't a class - if (!type.IsClass) - continue; - - // If the type doesn't implement the interface - var interfaceType = type.GetInterface(serializerName); - if (interfaceType == null) - continue; - - // If the interface doesn't use the correct type parameter - var genericTypes = interfaceType.GetGenericArguments(); - if (genericTypes.Length != 1 || genericTypes[0] != modelType) - continue; - - // Try to create a concrete instance of the type - var instance = (TSerializer?)Activator.CreateInstance(type); - if (instance != null) - return instance; - } - } - - return default; - } - - #endregion } }