-1

I'm trying to call ToDescription method on enum which is declared as T. Compiler won't accept this method as it doesn't know that T is an enum.

What I want to achieve is something like this:

public string FromDtoProperty(T source)
{
    return source.ToDescription();
}

Is there any way how to do this?

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • 1
    What is `ToDescription`? How this method defined? – Sergey Berezovskiy Aug 08 '14 at 20:28
  • 3
    What *is* the description of an enum value? In `enum E { A, B, C }` what is the description of `E.B`? – Timothy Shields Aug 08 '14 at 20:28
  • Is `ToDescription` an extension method for any given enum? – Yuval Itzchakov Aug 08 '14 at 20:30
  • I'm guessing he might mean this: [System.ComponentModel.DescriptionAttribute](http://msdn.microsoft.com/en-us/library/system.componentmodel.descriptionattribute(v=vs.110).aspx) – Timothy Shields Aug 08 '14 at 20:31
  • You can return an enum value using its type with the static `Enum` class. But a standard enum has no `ToDescription` property. – crthompson Aug 08 '14 at 20:32
  • This question does not make sense without further explanation. There is no `ToDescription` method on [enum](http://msdn.microsoft.com/en-us/library/system.enum%28v=vs.110%29.aspx). – Andrew Savinykh Aug 08 '14 at 20:42
  • @zespri - It's an extension method. – Neil Smith Aug 08 '14 at 20:45
  • @Smith.h.Neil that would be wrong too, there is no `ToDescription` extension method in BCL. Search MSDN if not sure. And if you (or OP) are using sone non-standard extension methods this information has to be included in the question, for the question to make sense. – Andrew Savinykh Aug 08 '14 at 20:48
  • @zespri - I meant it's his extension. I am assuming this is the case though... I sure hope it's his extension. – Neil Smith Aug 08 '14 at 20:51
  • 1
    Ideally, you could do `where T : enum`. But this is not supported in C# yet. =( http://stackoverflow.com/a/79903/781792 – Tim S. Aug 08 '14 at 20:52
  • @TimS. Good one! On a similar note, [that's why](http://stackoverflow.com/a/1331811/284111) – Andrew Savinykh Aug 08 '14 at 21:01
  • possible duplicate of [Create Generic method constraining T to an Enum](http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum) – John Alexiou Aug 08 '14 at 21:47
  • There is a standard way to constrain a generic type to enum. See possible duplicate reference. – John Alexiou Aug 08 '14 at 21:47

1 Answers1

2

Does that needs to be generic method? If not this should work.

public string FromDtoProperty(Enum source)
{
    return source.ToDescription();
}

If it has to be generic, then you can do

public string FromDtoProperty<T>(T source) where T : struct, IConvertible
{
    if (!typeof(T).IsEnum) 
    {
        throw new ArgumentException("T must be an enumerated type");
    }
    return ((Enum)(object)source).ToDescription();
}

Assuming ToDescription is an extension method defined for any Enum.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189