From 4cd52162eb3f8820d95b246a81c321d7c32074c1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 16 Sep 2025 22:25:40 -0400 Subject: [PATCH] Static implementations using reflection go away --- .../Deserializers/BaseBinaryDeserializer.cs | 112 +----------------- 1 file changed, 4 insertions(+), 108 deletions(-) diff --git a/SabreTools.Serialization/Deserializers/BaseBinaryDeserializer.cs b/SabreTools.Serialization/Deserializers/BaseBinaryDeserializer.cs index 0360db89..dd9c5c4e 100644 --- a/SabreTools.Serialization/Deserializers/BaseBinaryDeserializer.cs +++ b/SabreTools.Serialization/Deserializers/BaseBinaryDeserializer.cs @@ -1,6 +1,4 @@ -using System; using System.IO; -using System.Reflection; using SabreTools.Serialization.Interfaces; namespace SabreTools.Serialization.Deserializers @@ -9,7 +7,10 @@ namespace SabreTools.Serialization.Deserializers /// Base class for all binary deserializers /// /// Type of the model to deserialize - /// These methods assume there is a concrete implementation of the deserialzier for the model available + /// + /// This class allows all inheriting types to only implement + /// and still implicitly implement and + /// public abstract class BaseBinaryDeserializer : IByteDeserializer, IFileDeserializer, @@ -57,110 +58,5 @@ namespace SabreTools.Serialization.Deserializers public abstract TModel? Deserialize(Stream? data); #endregion - - #region Static Implementations - - /// - public static TModel? DeserializeBytes(byte[]? data, int offset) - { - var deserializer = GetType>(); - if (deserializer == null) - return default; - - return deserializer.Deserialize(data, offset); - } - - /// - public static TModel? DeserializeFile(string? path) - { - var deserializer = GetType>(); - if (deserializer == null) - return default; - - return deserializer.Deserialize(path); - } - - /// - public static TModel? DeserializeStream(Stream? data) - { - var deserializer = GetType>(); - if (deserializer == null) - return default; - - return deserializer.Deserialize(data); - } - - #endregion - - #region Helpers - - /// - /// Get a constructed instance of a type, if possible - /// - /// Deserializer type to construct - /// Deserializer of the requested type, null on error - private static TDeserializer? GetType() - { - // If the deserializer type is invalid - string? deserializerName = typeof(TDeserializer)?.Name; - if (deserializerName == null) - return default; - - // If the deserializer has no generic arguments - var genericArgs = typeof(TDeserializer).GetGenericArguments(); - if (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(deserializerName); - 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 = (TDeserializer?)Activator.CreateInstance(type); - if (instance != null) - return instance; - } - } - - return default; - } - - #endregion } }