NamespaceSystemLibrary.Common.Net.Extensions SystemLibrary.Common.Net.dll
Extension methods for Enums like ToText(), ToValue(), IsAny(), ...
EnumExtensions
public static class EnumExtensions
Inheritance
System.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
Methods arguments
Type | Name | Description |
---|---|---|
System.Enum | enumField |
X
Type | Description |
---|---|
System.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
Methods arguments
Type | Name | Description |
---|---|---|
System.Enum | enumField |
X
Type | Description |
---|---|
System.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)
X
enum Color {
Black,
White,
Red,
Blue
}
var red = Color.Red;
if(red.IsAny(Color.Black, Color.Blue)) {
//Never hit, red is Red, never blue/black
}
X
Methods arguments
Type | Name | Description |
---|---|---|
System.Enum | enumField | |
System.Enum[] | values |
X
Type | Description |
---|---|
System.Boolean | True or false |
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
Methods arguments
Type | Name | Description |
---|---|---|
System.Enum | enumField |
X
Type | Description |
---|---|
System.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
Methods arguments
Type | Name | Description |
---|---|---|
System.Enum | enumField |
X
Type | Description |
---|---|
System.String | Returns value of EnumValue attribute if exist, or Enum as string |
}