Enum ToString with user friendly strings

后端 未结 23 2079
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 11:44

My enum consists of the following values:

private enum PublishStatusses{
    NotCompleted,
    Completed,
    Error
};

I want to be able to

23条回答
  •  执笔经年
    2020-11-22 12:31

    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).

提交回复
热议问题