Handle known enumerable types better

This commit is contained in:
Matt Nadareski
2024-11-12 21:12:06 -05:00
parent 4b3955af77
commit a4da7f3657
19 changed files with 227 additions and 178 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
namespace SabreTools.Core.Tools
{
@@ -32,7 +31,7 @@ namespace SabreTools.Core.Tools
return null;
// Get the enum value info from the array, if possible
var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType);
var enumValueMemberInfo = Array.Find(memberInfos, m => m.DeclaringType == enumType);
if (enumValueMemberInfo == null)
return null;
@@ -42,7 +41,7 @@ namespace SabreTools.Core.Tools
return null;
// Return the first attribute, if possible
return (MappingAttribute?)attributes.FirstOrDefault();
return (MappingAttribute?)attributes[0];
}
}
}

View File

@@ -50,19 +50,23 @@ namespace SabreTools.Core.Tools
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// If not all types can be loaded, use the ones that could be
List<Type> assemblyTypes = [];
Type?[] assemblyTypes = [];
try
{
assemblyTypes = [.. assembly.GetTypes()];
assemblyTypes = assembly.GetTypes();
}
catch (ReflectionTypeLoadException rtle)
{
assemblyTypes = [.. rtle.Types.Where(t => t != null)];
assemblyTypes = Array.FindAll(rtle.Types ?? [], t => t != null);
}
// Loop through all types
foreach (Type type in assemblyTypes)
foreach (Type? type in assemblyTypes)
{
// If the type is invalid
if (type == null)
continue;
// If the type isn't a class or doesn't implement the interface
if (!type.IsClass || !typeof(DatItem).IsAssignableFrom(type))
continue;
@@ -89,19 +93,23 @@ namespace SabreTools.Core.Tools
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// If not all types can be loaded, use the ones that could be
List<Type> assemblyTypes = [];
Type?[] assemblyTypes = [];
try
{
assemblyTypes = [.. assembly.GetTypes()];
assemblyTypes = assembly.GetTypes();
}
catch (ReflectionTypeLoadException rtle)
{
assemblyTypes = [.. rtle.Types.Where(t => t != null)];
assemblyTypes = Array.FindAll(rtle.Types ?? [], t => t != null);
}
// Loop through all types
foreach (Type type in assemblyTypes)
foreach (Type? type in assemblyTypes)
{
// If the type is invalid
if (type == null)
continue;
// If the type isn't a class or doesn't implement the interface
if (!type.IsClass || !typeof(DatItem).IsAssignableFrom(type))
continue;