-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed namespace wrapper around EnumExtensions.
- Loading branch information
1 parent
5f90050
commit ca1de00
Showing
3 changed files
with
18 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreSystem.Sys | ||
public static class EnumExtensions | ||
{ | ||
public static class EnumExtensions | ||
/// <summary> | ||
/// Gets an attribute on an enum field value | ||
/// </summary> | ||
/// <typeparam name="T">The type of the attribute you want to retrieve</typeparam> | ||
/// <param name="enumVal">The enum value</param> | ||
/// <returns>The attribute of type T that exists on the enum value</returns> | ||
/// <example>string desc = myEnumVariable.GetAttributeOfType<DescriptionAttribute>().Description;</example> | ||
public static T GetAttributeOfType<T>(this Enum enumVal) | ||
where T : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets an attribute on an enum field value | ||
/// </summary> | ||
/// <typeparam name="T">The type of the attribute you want to retrieve</typeparam> | ||
/// <param name="enumVal">The enum value</param> | ||
/// <returns>The attribute of type T that exists on the enum value</returns> | ||
/// <example>string desc = myEnumVariable.GetAttributeOfType<DescriptionAttribute>().Description;</example> | ||
public static T GetAttributeOfType<T>(this Enum enumVal) | ||
where T : Attribute | ||
{ | ||
var type = enumVal.GetType(); | ||
var memInfo = type.GetMember(enumVal.ToString()); | ||
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false); | ||
return (attributes.Length > 0) ? (T)attributes[0] : null; | ||
} | ||
var type = enumVal.GetType(); | ||
var memInfo = type.GetMember(enumVal.ToString()); | ||
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false); | ||
return (attributes.Length > 0) ? (T)attributes[0] : null; | ||
} | ||
} |