System Library Common Framework
Search Results for

    *.Benchmark*,*.Tests*,*.ApiTests*,*.BaseTest*,DelegateJsonConverter,CalleeCancelledRequestException,ClientResponse,ContentType,OutputCachePolicy
    Show / Hide Table of Contents Show / Hide Table of Contents
    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
    Methods arguments
    Type Name Description
    Enum enumField
    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
    Methods arguments
    Type Name Description
    Enum enumField
    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)

    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
    Enum enumField
    Enum[] values
    X
    Type Description
    bool

    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
    Enum enumField
    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
    Methods arguments
    Type Name Description
    Enum enumField
    X
    Type Description
    string

    Returns value of EnumValue attribute if exist, or Enum as string

    }

    In This Article
    Package: nuget
    Source: github
    Website: System Library Common Framework