Skip to content

Commit

Permalink
handle EnumConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
karakasa committed Aug 13, 2023
1 parent 0b0a055 commit 8715cd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 11 additions & 1 deletion main/Util/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace NPOI.Util
{
/// <summary>
/// This class provides helper methods that reduce dynamic code / reflection use, for better AOT performance.
/// This class provides helper methods that reduce dynamic code / reflection use on Enums
/// </summary>
public static class EnumExtensions
{
Expand All @@ -27,6 +27,11 @@ public static string[] GetEnumNames<T>() where T : struct, Enum
{
return Enum.GetNames(typeof(T));
}

public static T Parse<T>(string name, bool ignoreCase = false) where T : struct, Enum
{
return (T)Enum.Parse(typeof(T), name, ignoreCase);
}
#else
// AOT-friendly
public static T[] GetEnumValues<T>() where T : struct, Enum
Expand All @@ -43,6 +48,11 @@ public static string[] GetEnumNames<T>() where T : struct, Enum
{
return Enum.GetNames<T>();
}

public static T Parse<T>(string name, bool ignoreCase = false) where T : struct, Enum
{
return Enum.Parse<T>(name, ignoreCase);
}
#endif
}
}
6 changes: 4 additions & 2 deletions ooxml/Util/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public static ParagraphAlignment ValueOf(ST_Jc val)
}
}
public static T ValueOf<T, F>(F val)
where T : struct, Enum
where F : struct, Enum
{
string value = Enum.GetName(val.GetType(), val);
return (T)Enum.Parse(typeof(T), value, true);
string value = EnumExtensions.GetEnumName(val);
return EnumExtensions.Parse<T>(value, true);
}
}
}

0 comments on commit 8715cd2

Please sign in to comment.