Skip to content

Commit

Permalink
enumeration extensions helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Jun 7, 2023
1 parent d567999 commit 95382dc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
6 changes: 6 additions & 0 deletions PlayGround/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
new(1, "Lucas", FooType.Foo1), new(2, "Teles", FooType.Foo2)
};

var values = Enum.GetValues(typeof(FooType)).GetEnumerator().ToEnumerable().ToArray();

var foo = EnumerationExtensions.GetDescription(values[0]!);
var bar = EnumerationExtensions.GetEnumMemberValue(values[1]!);
Console.WriteLine($"{foo} and {bar}");

var json = JsonSerializer.Serialize(persons);
Console.WriteLine(json);

Expand Down
10 changes: 10 additions & 0 deletions src/EnumerablePlus/LinqEnumerablePlus.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

Expand Down Expand Up @@ -271,4 +272,13 @@ public static IEnumerable<T> ToEnumerable<T>(this IEnumerator<T> enumerator)
while (enumerator.MoveNext())
yield return enumerator.Current;
}

/// <summary>
/// Creates an IEnumerable from an IEnumerator
/// </summary>
public static IEnumerable<object?> ToEnumerable(this IEnumerator enumerator)
{
while (enumerator.MoveNext())
yield return enumerator.Current;
}
}
38 changes: 33 additions & 5 deletions src/EnumerationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@
/// </summary>
public static class EnumerationExtensions
{
static object? GetAttribute(Type enumType, Type attributeType, string? value) =>
value is null || !enumType.IsEnum
? null
: enumType
.GetMember(value)
.SelectMany(p => p.GetCustomAttributes(attributeType, true))
.FirstOrDefault();

static TAttr? GetAttribute<TEnum, TAttr>(this TEnum value)
where TEnum : Enum
where TAttr : Attribute =>
value
.GetType()
.GetMember(value.ToString())
.SelectMany(p => p.GetCustomAttributes(typeof(TAttr), true))
.FirstOrDefault() as TAttr;
GetAttribute(value.GetType(), typeof(TAttr), value.ToString()) as TAttr;

/// <summary>
/// Get description from EnumMember.Value Attribute
/// </summary>
public static string? GetEnumMemberValue(object @enum) =>
GetAttribute(@enum.GetType(), typeof(EnumMemberAttribute), @enum.ToString()) is
EnumMemberAttribute
{
Value: { } description,
}
? description
: @enum.ToString();

/// <summary>
/// Get enum description from EnumMember.Value Attribute
Expand All @@ -41,6 +57,18 @@ public static class EnumerationExtensions
: @enum.ToString();


/// <summary>
/// Get description from DescriptionAttribute
/// </summary>
public static string? GetDescription(object @enum) =>
GetAttribute(@enum.GetType(), typeof(DescriptionAttribute), @enum.ToString()) is
DescriptionAttribute
{
Description: { } description,
}
? description
: @enum.ToString();

static TEnum? GetEnumByString<TEnum>(
string enumDescription,
Func<TEnum, string> getString,
Expand Down

0 comments on commit 95382dc

Please sign in to comment.