Re: Custom Attributes, Shared methods, Derived classes and reflection



On 31 Jul 2006 10:19:57 -0700, tommaso.gastaldi@xxxxxxxxxxx wrote:

Ok let's make another attempt... :)

What do you think of this ? (at least the result is that wanted)
It only requires an additional statement in the derived class:

Well, that does indeed give the result wanted.

Please don't take this the wrong way, because I really do appreciate your help, but
it does make me think that it's a bit of hackery. (which is not necessarily a
prejorative term :-)

I guess the problem that I have with it is that it requires the developer of the
derived class to deal with the friendly name issue on three separate fronts. He must
provide the attribute on the class definition. He must define a constant within a
class that is the type of the very class he is defining. He must provide this
constant in every call to the FriendlyName method.

As I alluded to in an earlier post, I have in fact dealt with this issue in the past
by defining a FriendlyName (shared) method in the derived class. The base class
defined a MustInherit FriendlyName method, and each derived class in turn implemented
the method and returned the appropriate string.

I noticed, however, that the majority of the classes we defined had their
FriendlyName the same as their class name (e.g.'Customer'), so this resulted in a lot
of superfluous methods. I then got this bright idea to use an attribute instead. The
idea is that the developer would include the attribute only if a friendly name was
different than the class name. It seems to me that it is much simpler to define the
attribute when needed, and then reference the shared method "through" the derived
class. If no attribute was present, then the FriendlyName method would simply return
the ClassName.

Unfortunately I'm unable to find the magic incantation using reflection to do this.
Clearly, the runtime is in fact aware that MyDerivedClass inherits from MyBaseClass.
This is because MyDerivedClass.FriendlyName in fact invokes the FriendlyName method
in MyBaseClass. I was hoping that given that, there was a way to reflect "back" from
the base class to the derived class that invoked the method in it, without having an
instance of the derived class available to get the derived type.

sigh.


-- Jeff
.