mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-05 22:01:33 +00:00
AttributeHelper code was only ever used in converters
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace SabreTools.Metadata
|
||||
{
|
||||
public static class AttributeHelper<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the MappingAttribute from a supported value
|
||||
/// </summary>
|
||||
/// <param name="value">Value to use</param>
|
||||
/// <returns>MappingAttribute attached to the value</returns>
|
||||
public static MappingAttribute? GetMappingAttribute(T? value)
|
||||
{
|
||||
// Null value in, null value out
|
||||
if (value is null)
|
||||
return null;
|
||||
|
||||
// Current enumeration type
|
||||
var enumType = typeof(T);
|
||||
if (Nullable.GetUnderlyingType(enumType) is not null)
|
||||
enumType = Nullable.GetUnderlyingType(enumType);
|
||||
|
||||
// If the value returns a null on ToString, just return null
|
||||
string? valueStr = value.ToString();
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
return null;
|
||||
|
||||
// Get the member info array
|
||||
var memberInfos = enumType?.GetMember(valueStr);
|
||||
if (memberInfos is null)
|
||||
return null;
|
||||
|
||||
// Get the enum value info from the array, if possible
|
||||
var enumValueMemberInfo = Array.Find(memberInfos, m => m.DeclaringType == enumType);
|
||||
if (enumValueMemberInfo is null)
|
||||
return null;
|
||||
|
||||
// Try to get the relevant attribute
|
||||
var attributes = enumValueMemberInfo.GetCustomAttributes(typeof(MappingAttribute), true);
|
||||
if (attributes is null || attributes.Length == 0)
|
||||
return null;
|
||||
|
||||
// Return the first attribute, if possible
|
||||
return (MappingAttribute?)attributes[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ namespace SabreTools.Metadata
|
||||
continue;
|
||||
|
||||
// Try to get the mapping attribute
|
||||
MappingAttribute? attr = AttributeHelper<T>.GetMappingAttribute(value);
|
||||
MappingAttribute? attr = GetMappingAttribute(value);
|
||||
if (attr?.Mappings is null || attr.Mappings.Length == 0)
|
||||
continue;
|
||||
|
||||
@@ -106,7 +106,7 @@ namespace SabreTools.Metadata
|
||||
continue;
|
||||
|
||||
// Try to get the mapping attribute
|
||||
MappingAttribute? attr = AttributeHelper<T>.GetMappingAttribute(value);
|
||||
MappingAttribute? attr = GetMappingAttribute(value);
|
||||
if (attr?.Mappings is null || attr.Mappings.Length == 0)
|
||||
continue;
|
||||
|
||||
@@ -128,5 +128,49 @@ namespace SabreTools.Metadata
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Get the MappingAttribute from a supported value
|
||||
/// </summary>
|
||||
/// <param name="value">Value to use</param>
|
||||
/// <returns>MappingAttribute attached to the value</returns>
|
||||
internal static MappingAttribute? GetMappingAttribute<T>(T? value)
|
||||
{
|
||||
// Null value in, null value out
|
||||
if (value is null)
|
||||
return null;
|
||||
|
||||
// Current enumeration type
|
||||
var enumType = typeof(T);
|
||||
if (Nullable.GetUnderlyingType(enumType) is not null)
|
||||
enumType = Nullable.GetUnderlyingType(enumType);
|
||||
|
||||
// If the value returns a null on ToString, just return null
|
||||
string? valueStr = value.ToString();
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
return null;
|
||||
|
||||
// Get the member info array
|
||||
var memberInfos = enumType?.GetMember(valueStr);
|
||||
if (memberInfos is null)
|
||||
return null;
|
||||
|
||||
// Get the enum value info from the array, if possible
|
||||
var enumValueMemberInfo = Array.Find(memberInfos, m => m.DeclaringType == enumType);
|
||||
if (enumValueMemberInfo is null)
|
||||
return null;
|
||||
|
||||
// Try to get the relevant attribute
|
||||
var attributes = enumValueMemberInfo.GetCustomAttributes(typeof(MappingAttribute), true);
|
||||
if (attributes is null || attributes.Length == 0)
|
||||
return null;
|
||||
|
||||
// Return the first attribute, if possible
|
||||
return (MappingAttribute?)attributes[0];
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user