diff --git a/SabreTools.Metadata.Filter/FilterKey.cs b/SabreTools.Metadata.Filter/FilterKey.cs index 6ecda77e..7666d353 100644 --- a/SabreTools.Metadata.Filter/FilterKey.cs +++ b/SabreTools.Metadata.Filter/FilterKey.cs @@ -1,5 +1,6 @@ using System; using System.Reflection; +using System.Xml.Serialization; using SabreTools.Data.Models.Metadata; namespace SabreTools.Metadata.Filter @@ -202,7 +203,7 @@ namespace SabreTools.Metadata.Filter private static string? GetMatchingField(string itemName, string fieldName) { // Get the correct item type - var itemType = TypeHelper.GetDatItemType(itemName.ToLowerInvariant()); + var itemType = GetDatItemType(itemName.ToLowerInvariant()); if (itemType is null) return null; @@ -216,6 +217,59 @@ namespace SabreTools.Metadata.Filter return propertyMatch?.ToLowerInvariant(); } + #region Reflection-based Helpers + + /// + /// Attempt to get the DatItem type from the name + /// + private static Type? GetDatItemType(string? itemType) + { + if (string.IsNullOrEmpty(itemType)) + return null; + + // Loop through all loaded assemblies + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + // If not all types can be loaded, use the ones that could be + Type?[] assemblyTypes = []; + try + { + assemblyTypes = assembly.GetTypes(); + } + catch (ReflectionTypeLoadException rtle) + { + assemblyTypes = Array.FindAll(rtle.Types ?? [], t => t is not null); + } + + // Loop through all types + foreach (Type? type in assemblyTypes) + { + // If the type is invalid + if (type is null) + continue; + + // If the type isn't a class or doesn't implement the interface + if (!type.IsClass || !typeof(DatItem).IsAssignableFrom(type)) + continue; + + // Get the XML type name +#if NET20 || NET35 || NET40 + string? elementName = (Attribute.GetCustomAttribute(type, typeof(XmlRootAttribute)) as XmlRootAttribute)!.ElementName; +#else + string? elementName = type.GetCustomAttribute()?.ElementName; +#endif + if (elementName is null) + continue; + + // If the name matches + if (string.Equals(elementName, itemType, StringComparison.OrdinalIgnoreCase)) + return type; + } + } + + return null; + } + /// /// Get property names for the given type, if possible /// @@ -231,5 +285,7 @@ namespace SabreTools.Metadata.Filter string[] propertyNames = Array.ConvertAll(properties, f => f.Name); return Array.FindAll(propertyNames, s => s.Length > 0); } + + #endregion } } diff --git a/SabreTools.Metadata/TypeHelper.cs b/SabreTools.Metadata/TypeHelper.cs deleted file mode 100644 index 1f6b3475..00000000 --- a/SabreTools.Metadata/TypeHelper.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Reflection; -using System.Xml.Serialization; -using SabreTools.Data.Models.Metadata; - -namespace SabreTools.Metadata -{ - // TODO: Investigate ways of either caching or speeding up these methods - public static class TypeHelper - { - /// - /// Attempt to get the DatItem type from the name - /// - public static Type? GetDatItemType(string? itemType) - { - if (string.IsNullOrEmpty(itemType)) - return null; - - // Loop through all loaded assemblies - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - // If not all types can be loaded, use the ones that could be - Type?[] assemblyTypes = []; - try - { - assemblyTypes = assembly.GetTypes(); - } - catch (ReflectionTypeLoadException rtle) - { - assemblyTypes = Array.FindAll(rtle.Types ?? [], t => t is not null); - } - - // Loop through all types - foreach (Type? type in assemblyTypes) - { - // If the type is invalid - if (type is null) - continue; - - // If the type isn't a class or doesn't implement the interface - if (!type.IsClass || !typeof(DatItem).IsAssignableFrom(type)) - continue; - - // Get the XML type name - string? elementName = GetXmlRootAttributeElementName(type); - if (elementName is null) - continue; - - // If the name matches - if (string.Equals(elementName, itemType, StringComparison.OrdinalIgnoreCase)) - return type; - } - } - - return null; - } - - /// - /// Attempt to get the XmlRootAttribute.ElementName value from a type - /// - public static string? GetXmlRootAttributeElementName(Type? type) - { - if (type is null) - return null; - -#if NET20 || NET35 || NET40 - return (Attribute.GetCustomAttribute(type, typeof(XmlRootAttribute)) as XmlRootAttribute)!.ElementName; -#else - return type.GetCustomAttribute()?.ElementName; -#endif - } - } -}