Re: Passing enumerator as generic type
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Wed, 28 Nov 2007 13:37:12 -0600
Since ImageSelection turns in MyImages it should work? But the compiler is
unfortunately treating it like a generic object. Maybe I can use
reflection to get the value of the enum? (I'd like a compile time solution
if possible.
Since you can't have a static interface in .NET (enum constants are static
members), you can't do this compile-time.
If you use reflection you probably want to cache the result, then your
performance hit won't be so bad and you can regain some type safety by
encapsulating the casts.
something like:
static class DefaultFor<TEnum>
{
private static initonly TEnum defvalue;
static DefaultFor()
{
// extra error-checking might be good
defvalue = (TEnum) typeof(TEnum).GetFields("Default",
MemberBinding.Static | MemberBinding.Public)[0].GetValue(null);
}
public static TEnum Value { get { return defvalue; } }
}
Now you can refer to DefaultFor<TEnum>.Value instead of TEnum.Default, a
little more clunky but not bad.
Thanks,
Jon
.
- References:
- Passing enumerator as generic type
- From: Jon Slaughter
- Re: Passing enumerator as generic type
- From: Jon Slaughter
- Passing enumerator as generic type
- Prev by Date: Re: foreach, IEnumerable and modifying contents
- Next by Date: Re: Ignored advice, am now in serious poo on graphics
- Previous by thread: Re: Passing enumerator as generic type
- Next by thread: Play media files
- Index(es):
Relevant Pages
|