0

在Java中,我们可以enums这样声明

public enum ChangeFlag
{
    NEW("New"),
    DELETED("Deleted"),
    REVISED("Revised");

    private final String    text;

    ChangeFlag(String text)
    {
        this.text = text;
    }

    public String toString()
    {
        return text;
    }
}

C# 有什么优雅的等价物吗?

编辑:

public static class FooExtensions
{
public static string GetDescription(this Foo @this)
{
    switch (@this)
    {
        case Foo.Bar:
            return "A bar";
    }
}

}

你能解释一下 GetDescription(this Foo @this) 中的参数是什么意思吗?我不习惯看到this Foo @this指的是什么

4

3 回答 3

5

您可以使用DescriptionAttribute

public enum Foo
{
    [Description("A bar")]
    Bar
}

然后您将通过TypeDescriptor.GetAttributesor提取它Type.GetCustomAttributes

或者您可以使用扩展方法:

public enum Foo
{
    Bar
}

public static class FooExtensions
{
    public static string GetDescription(this Foo @this)
    {
        switch (@this)
        {
            case Foo.Bar:
                return "A bar";
        }
    }
}

// consuming code
var foo = Foo.Bar;
var description = foo.GetDescription();

后一种方法还可以让您在本地化方面获得更多控制权,因为您总是可以在资源文件中查找描述,例如。

于 2013-09-04T23:38:32.287 回答
2
internal static class Program
{
    private static void Main(string[] args)
    {
        ChangeFlag changeFlag = ChangeFlag.REVISED;
        Console.WriteLine(changeFlag.GetDescription());
        Console.Read();
    }

    public enum ChangeFlag
    {
        [Description("New")]
        NEW,
        [Description("Deleted")]
        DELETED,
        [Description("Revised")]
        REVISED
    }
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        if (fi != null)
        {
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }

        return value.ToString();
    }
}
于 2013-09-04T23:57:20.163 回答
2

枚举描述 getter 扩展方法的紧凑且防错的实现变体:

/// <exception cref="AmbiguousMatchException">More attributes found. </exception>
/// <exception cref="TypeLoadException">Cannot load custom attribute.</exception>
public static string GetDescription(this Enum value)
{
    Type type = value.GetType();
    string enumName = Enum.GetName(type, value);
    if (enumName == null)
    {
        return null; // or return string.Empty;
    }
    FieldInfo typeField = type.GetField(enumName);
    if (typeField == null)
    {
        return null; // or return string.Empty;
    }
    var attribute = Attribute.GetCustomAttribute(typeField, typeof(DescriptionAttribute));
    return (attribute as DescriptionAttribute)?.Description; // ?? string.Empty maybe added
}

此外,描述的比较扩展方法(使用上部)可能有用:

public static bool DescriptionEquals(this Enum value, string inputForComparison,
    StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
{
    string description;
    try
    {
        description = value.GetDescription();
    }
    catch (Exception ex) when (ex is AmbiguousMatchException || ex is TypeLoadException)
    {
        return false;
    }
    if (description == null || inputForComparison == null)
    {
        return false;
    }
    return inputForComparison.Equals(description, comparisonType);
    //  || inputForComparison.Equals(value.ToString(), comparisonType); may be added
}
于 2016-11-25T10:07:15.257 回答