Enum ToString with user friendly strings

2018-12-31 12:37发布

My enum consists of the following values:

private enum PublishStatusses{
    NotCompleted,
    Completed,
    Error
};

I want to be able to output these values in a user friendly way though.
I don't need to be able to go from string to value again.

19条回答
爱死公子算了
2楼-- · 2018-12-31 13:06

The simplest way is just to include this extension class into your project, it will work with any enum in the project:

public static class EnumExtensions
{
    public static string ToFriendlyString(this Enum code)
    {
        return Enum.GetName(code.GetType(), code);
    }
}

Usage:

enum ExampleEnum
{
    Demo = 0,
    Test = 1, 
    Live = 2
}

...

ExampleEnum ee = ExampleEnum.Live;
Console.WriteLine(ee.ToFriendlyString());
查看更多
浮光初槿花落
3楼-- · 2018-12-31 13:06

Instead of using an enum use a static class.

replace

private enum PublishStatuses{
    NotCompleted,
    Completed,
    Error
};

with

private static class PublishStatuses{
    public static readonly string NotCompleted = "Not Completed";
    public static readonly string Completed = "Completed";
    public static readonly string Error = "Error";
};

it will be used like this

PublishStatuses.NotCompleted; // "Not Completed"

Issue using the top "extension method" solutions:

A private enum is often used inside another class. The extension method solution is not valid there since it must be in it's own class. This solution can be private and embedded in another class.

查看更多
看淡一切
4楼-- · 2018-12-31 13:08

I think the best (and easiest) way to solve your problem is to write an Extension-Method for your enum:

public static string GetUserFriendlyString(this PublishStatusses status)
    {

    }
查看更多
听够珍惜
5楼-- · 2018-12-31 13:09

Maybe I'm missing something, but what's wrong with Enum.GetName?

public string GetName(PublishStatusses value)
{
    return Enum.GetName(typeof(PublishStatusses), value)
}

edit: for user-friendly strings, you need to go through a .resource to get internationalisation/localisation done, and it would arguably be better to use a fixed key based on the enum key than a decorator attribute on the same.

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 13:11

The easiest solution here is to use a custom extension method (in .NET 3.5 at least - you can just convert it into a static helper method for earlier framework versions).

public static string ToCustomString(this PublishStatusses value)
{
    switch(value)
    {
        // Return string depending on value.
    }
    return null;
}

I am assuming here that you want to return something other than the actual name of the enum value (which you can get by simply calling ToString).

查看更多
临风纵饮
7楼-- · 2018-12-31 13:11

For flags enum including.

    public static string Description(this Enum value)
    {
        Type type = value.GetType();

        List<string> res = new List<string>();
        var arrValue = value.ToString().Split(',').Select(v=>v.Trim());
        foreach (string strValue in arrValue)
        {
            MemberInfo[] memberInfo = type.GetMember(strValue);
            if (memberInfo != null && memberInfo.Length > 0)
            {
                object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0 && attrs.Where(t => t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault() != null)
                {
                    res.Add(((DescriptionAttribute)attrs.Where(t => t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault()).Description);
                }
                else
                    res.Add(strValue);
            }
            else
                res.Add(strValue);
        }

        return res.Aggregate((s,v)=>s+", "+v);
    }
查看更多
登录 后发表回答