-4

that question doesnt answer my question, as I need toString to be called, is there an extention method that can be added?

I have an Enum

public enum TimetableState
{
    ["Error Message"]
    errormessage = 0,
    Great = 1
}

I want to then call

TimetableState.errormessage.ToString();

and it display the string in the attribute 'Error Message', or if i call Great.ToString() the string 'Great' is returned.

the attribute can be anything, it does not have to be description.

I know it is possible to get this to work with ToString() directly, but how?

+++++++ This is not a duplicate, as I want to get the same as that but by calling ToString() on the enum, as I need a list sort and display to use it. is this possible? ++++++++

f1wade
  • 2,877
  • 6
  • 27
  • 43
  • the string description is associated with `TimetableState.errormessage` or 0; it will never display "Great" because it is a different value/member (1) – Ňɏssa Pøngjǣrdenlarp Nov 12 '14 at 13:01
  • @TonyTheLion - Yes quite a bit, the only thing i have seen is the getdescription, (answer 1) below, but we want to just call toString, as we want it to be easy to use for others, as a dll. – f1wade Nov 12 '14 at 13:02
  • @Plutonix - I have edited the original question to confirm, thanks. – f1wade Nov 12 '14 at 13:03
  • @f1wade: you don't need to shout. I have not answered your question, correct. I have closed it as duplicate of the other. What is this questions unique characteristic in your opinion? Btw, you cannot use `ToString` as you've stated to get it working because that returns `errormessage` not `"Error Message"`. And `GETDESCRIPTION` is not an available method that you can use but one that you have to implement like the one in the duplicate. – Tim Schmelter Nov 12 '14 at 13:10
  • [You can use a method](http://stackoverflow.com/a/25477800/1070452) which returns the Name as the default when there is no description (same as in the dupe). I dont know how else you could get the Description without using `GetDescription` - it is an attribute - meta data; maybe you want a shared string constant instead? – Ňɏssa Pøngjǣrdenlarp Nov 12 '14 at 13:12
  • thats sounds a bit better, but is there a way i can use the attributes, or without adding attributes to get it to change the tostring output? shared string constant would be seperate i need a linked enum. – f1wade Nov 12 '14 at 18:26
  • I think there is a way to extend the enum and reimplement the ToString method, but not sure how, can anyone enlighten me please? – f1wade Jul 01 '16 at 14:31

1 Answers1

1

Update: - never mind my answer, here is what you're looking for: How to get C# Enum description from value?

I didn't find a solution for it and wrote a helper function myself:

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

        if (fi != null)
        {
            var att = fi.GetCustomAttribute<DescriptionAttribute>(true);

            if (att != null)
                ret = att.Description;
        }

        return ret;
    }

This was ~3 years ago, if there is a built in solution I missed or there is one now, I'd gladly use it instead.

Community
  • 1
  • 1
peter
  • 14,348
  • 9
  • 62
  • 96