Re: values() of an Enumeration type that is a generic parameter?



On May 13, 6:28 pm, Lew <l...@xxxxxxxxxxxxxxxxxxxx> wrote:
Lasse Reichstein Nielsen wrote:
Actually, the Class<T> object has the method "T[] getEnumConstants()"
that returns the values of an enum type (or null, if the Class is not
an enum), so reflection isn't needed anyway.

That /is/ reflection.

Hummmm... not exactly. getEnumConstants() is only available (i.e.
returns something other than null)
when the class *is* an Enum class. Not really the behavior you expect
from reflection, that should be
independant from hypotheses on the nature of the class, and report
type-checking failures through
a few, well-defined runtime exceptions.

In my opinion, this is rather a hack to implement inheritance-aware
static fields,
in the particular case of <E extends Enum<E>> E
valueOf(Class<E>,String). Look at the implementation of Enum-related
methods in Class... you could do the same thing with a static
Map<Class<? extends Enum>,Enum[]>,
that would let you implement <E extends Enum<E>> E[] values(Class<E>),
but with much less performance.
And what object is Class-common, but updated/able whith inheritance?
The Class object, of course!

--
YLS

.



Relevant Pages

  • Re: Datatype of an enum
    ... >> enumeration without using reflection. ... If you have an enum variable, ... The thing is I'm trying to create a groupbox that acts much like the ... // The InitializeComponentcall is required for Windows Forms designer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Static property of type parameter - is this a CLR or C# limitation?
    ... I'll go for a combination of reflection and static fields. ... the constructors with factory parameters, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reflection, Enums and their values?
    ... reference to Q001 when you're building your library; however, you still don't need to use reflection. ... that you need (Just realize that the Enum methods themselves probably use reflection, ...
    (microsoft.public.dotnet.framework)
  • Re: Passing enumerator as generic type
    ... (I'd like a compile time solution ... Since you can't have a static interface in .NET (enum constants are static ... If you use reflection you probably want to cache the result, ... private static initonly TEnum defvalue; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Advantage of const against static readonly?
    ... >>You aren't worried about the memory consumption of a static readonly value ... > built on readonly static fields instead of consts you could probably ... remove the definition of the enum from the library it is using. ... But I have indeed found one reason why const can be better than static ...
    (microsoft.public.dotnet.languages.csharp)