Remove one reflection-based helper

This commit is contained in:
Matt Nadareski
2026-04-05 11:21:56 -04:00
parent d8e4f963c0
commit 15794e07f7
3 changed files with 12 additions and 47 deletions

View File

@@ -21,7 +21,11 @@ namespace SabreTools.Metadata.Filter
/// <summary>
/// Cached item type names for filter selection
/// </summary>
private static readonly string[] _datItemTypeNames = TypeHelper.GetDatItemTypeNames();
#if NET5_0_OR_GREATER
private static readonly string[] _datItemTypeNames = Enum.GetNames<ItemType>();
#else
private static readonly string[] _datItemTypeNames = Enum.GetNames(typeof(ItemType));
#endif
/// <summary>
/// Validating combined key constructor

View File

@@ -17,7 +17,11 @@ namespace SabreTools.Metadata.Filter
/// <summary>
/// Cached item type names for filter selection
/// </summary>
private static readonly string[] _datItemTypeNames = TypeHelper.GetDatItemTypeNames();
#if NET5_0_OR_GREATER
private static readonly string[] _datItemTypeNames = Enum.GetNames<ItemType>();
#else
private static readonly string[] _datItemTypeNames = Enum.GetNames(typeof(ItemType));
#endif
public FilterRunner(FilterObject[] filters)
{
@@ -38,7 +42,7 @@ namespace SabreTools.Metadata.Filter
{
Header => "header",
Machine => "machine",
DatItem => TypeHelper.GetXmlRootAttributeElementName(obj.GetType()),
DatItem datItem => datItem.ItemType.ToString(),
_ => null,
};
@@ -52,7 +56,7 @@ namespace SabreTools.Metadata.Filter
// Skip filters not applicable to the item
if (filterKey.StartsWith("item.") && Array.IndexOf(_datItemTypeNames, itemName) == -1)
continue;
else if (!filterKey.StartsWith("item.") && !filterKey.StartsWith(itemName))
else if (!filterKey.StartsWith("item.") && !filterKey.StartsWith(itemName, StringComparison.OrdinalIgnoreCase))
continue;
// If we don't get a match, it's a failure

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;
using SabreTools.Data.Models.Metadata;
@@ -9,48 +8,6 @@ namespace SabreTools.Metadata
// TODO: Investigate ways of either caching or speeding up these methods
public static class TypeHelper
{
/// <summary>
/// Attempt to get all DatItem types
/// </summary>
public static string[] GetDatItemTypeNames()
{
List<string> typeNames = [];
// 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 not null)
typeNames.Add(elementName);
}
}
return [.. typeNames];
}
/// <summary>
/// Attempt to get the DatItem type from the name
/// </summary>