NamespaceSystemLibrary.Common.Framework.Extensions SystemLibrary.Common.Framework.Net.dll
Extension methods for Enums like ToText(), ToValue(), IsAny(), ...
EnumExtensions
public static class EnumExtensions
Inheritance
object
EnumExtensions
Examples
Arguments
X
Methods
Gets the EnumText attribute's object value
public static string GetEnumText(this Enum enumField)
X
enum EnumColor
{
[EnumText("White")]
[EnumValue(1234)]
Black,
[EnumText((string)null)]
Red,
Pink
}
var value = EnumColor.Black.GetEnumText();
// "White"
var value = EnumColor.Red.GetEnumText();
// null
var value = EnumColor.Pink.GetEnumText();
// null
X
Type | Description |
---|---|
string | Returns EnumText attributes value or null if not defined |
Gets the EnumValue-attribute's object value
public static object GetEnumValue(this Enum enumField)
X
enum EnumColor
{
[EnumText("White")]
[EnumValue(1234)]
Black,
[EnumValue(null)]
Blue,
Pink
}
var value = EnumColor.Black.GetEnumValue();
// 1234, an int
var value = EnumColor.Pink.GetEnumValue();
// null as Pink does not have the EnumValue attribute
var value = EnumColor.Blue.GetEnumValue();
// null as Blue have null as the EnumValue
X
Type | Description |
---|---|
object | Returns EnumValue attribute's value or null if not exist |
Check if Enum is matching any in the array
public static bool IsAny(this Enum enumField, params Enum[] values)
Gets the EnumText attribute's value, fallback to enumField.ToString()
public static string ToText(this Enum enumField)
X
enum EnumColor
{
[EnumText("White")]
[EnumValue("BlackAndWhite")]
Black,
Pink
}
var value = EnumColor.Black.ToText();
// White
var value = EnumColor.Pink.ToText();
// Pink
X
Type | Description |
---|---|
string | Returns the text in EnumText attribute or Enum as string or null if nullwas passed |
Gets the EnumValue attribute's value, fallback to enumField.ToString()
public static string ToValue(this Enum enumField)
X
enum EnumColor
{
[EnumText("White")]
[EnumValue(1234)]
Black,
[EnumValue(null)]
Blue,
Pink
}
var value = EnumColor.Black.ToValue();
// "1234" as string
var value = EnumColor.Pink.ToValue();
// Pink, does not have the EnumValue attribute so it falls back to Pink as a string
var value = EnumColor.Blue.ToValue();
// value is null, as Blue have EnumValue null
X
Type | Description |
---|---|
string | Returns value of EnumValue attribute if exist, or Enum as string |
}