mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-07-08 18:06:41 +00:00
Remove one reflection-based helper
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user