Closed GoogleCodeExporter closed 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
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
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
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
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
Original comment by jonathan.skeet
on 24 Aug 2012 at 6:19
Original issue reported on code.google.com by
jason.ch...@gmail.com
on 17 Dec 2009 at 8:37