EnumHelper.get_values returns a cached strongly typed IReadOnlyList<T> (simple optimization)

This commit is contained in:
John Colvin committed 2019-05-19 17:44:34 -05:00
1 parent af64592f52
commit 57b70684ea
1 file changed
+6 -2
+6 -2
View File
@@ -10,9 +10,11 @@ using System.Reflection;
namespace mbts.ui.utility {
static class EnumHelper {
public static IEnumerable<T> get_values<T>() { return Enum.GetValues(typeof(T)).Cast<T>(); }
public static IReadOnlyList<T> get_values<T>() =>
EnumDescriptorStore<T>.values;
public static IEnumerable<T> get_values_except<T>(params T[] except) { return get_values<T>().Except(except); }
public static IEnumerable<T> get_values_except<T>(params T[] except) =>
get_values<T>().Except(except);
public static IEnumerable get_values(Type t) {
if (!t.IsEnum) {
@@ -115,6 +117,8 @@ namespace mbts.ui.utility {
static class EnumDescriptorStore<T> {
public static readonly EnumDescriptor descriptor;
public static readonly IReadOnlyList<T> values = Enum.GetValues(typeof(T)).Cast<T>().ToReadOnlyList();
static EnumDescriptorStore() {
Func<object, object, bool> has_flags;
if (!typeof(T).IsEnum) {