jskeet / unconstrained-melody

Automatically exported from code.google.com/p/unconstrained-melody
131 stars 17 forks source link

Does this work for extension methods? #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to use the IEnumConstaint on an extension method, but when I try
to use the extension method on an Enum I get the following error:

Error   4   The type 'MyEnum' cannot be used as type parameter 'T' in the
generic type or method 'xxxx.EnumExtensions.ParseDescription<T>(T,
string)'. There is no boxing conversion from 'MyEnum' to
'UnconstrainedMelody.IEnumConstraint'.  

Thanks.  This is a really cool project, could be very useful.

Original issue reported on code.google.com by jason.ch...@gmail.com on 17 Dec 2009 at 8:37

GoogleCodeExporter commented 9 years ago
You don't apply IEnumConstraint onto your own code - it's an internal part of 
Unconstrained Melody, effectively. Of course, you could always use the same 
rewriter 
in your own code - but it would have to be in a different assembly to the 
calling 
code.

Is this something sufficiently general purpose that it would be better off just 
being 
included in Unconstrained Melody itself?

Original comment by jonsk...@google.com on 17 Dec 2009 at 8:53

GoogleCodeExporter commented 9 years ago
I was afraid of that.  I don't think the method is that general - although I 
guess I 
can let you decide that.  It's an extension method that takes a string value 
and 
attempts to match it to a Description attribute applied to an enum value, and 
then 
returns that value if found.  Unfortunately, without being able to constrain 
the 
type to an Enum, I'm stuck with a method only contrained to structs - that 
won't 
work on non-Enums.  

At least I know about your library now, I'll have to check out what features it 
has 
that I might want to use.  Thanks!  

Original comment by jason.ch...@gmail.com on 18 Dec 2009 at 4:33

GoogleCodeExporter commented 9 years ago
Does Enums.TryParseDescription not work for you? That's already in 
unconstrained melody 
:)

Original comment by jonathan.skeet on 21 Dec 2009 at 8:33

GoogleCodeExporter commented 9 years ago
Oh, yeah, I see that now in the source, but it wasn't in the build I 
downloaded.  It
would work pretty well for what I need.  Thanks. 

Original comment by jason.ch...@gmail.com on 22 Dec 2009 at 5:01

GoogleCodeExporter commented 9 years ago
Added the following method for my purposes.  You are free to use it (or a 
variation
of it) if it works for you.  It's an extension method on Enums that checks a 
string
against either a description attribute or a name.  If neither matches it 
returns the
original enum (i.e. a default value).  

        /// <summary>
        /// Parses a string to see if it matches a enum name or description attribute.
        /// If no match is found, this method returns the original value
        /// </summary>
        /// <param name="enumValue">Default enum value</param>
        /// <param name="parseValue">String value to attempt to parse into enum
value</param>
        /// <returns><see cref="System.Enum"/></returns>
        public static T ParseNameOrDescription<T>(this T enumValue, string
parseValue) where T : struct, IEnumConstraint
        {
            Type enumType = enumValue.GetType();
            T originalValue = enumValue;

            if (!Enums.TryParseDescription<T>(parseValue, out enumValue))
                if (!Enums.TryParseName<T>(parseValue, out enumValue))
                    enumValue = originalValue;

            return enumValue;
        }

Original comment by jason.ch...@gmail.com on 22 Dec 2009 at 5:59

GoogleCodeExporter commented 9 years ago

Original comment by jonathan.skeet on 24 Aug 2012 at 6:19