Custom Attributes, Shared methods, Derived classes and reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tommaso.gastaldi@uniroma1.it

    #16
    Re: Custom Attributes, Shared methods, Derived classes and reflect

    Very good, Jeff.

    Probably there is no way to escape the need of passing info about
    the type, because, as you rightly noted, reflection works with actual
    instances (unless we are missing something). And your way is an elegant
    way to do it.

    Why, sometime, don't you organize this information on a web page. I
    think it would
    be of interest to many people. :)

    -Tom

    Jeff Mason (Nospam) ha scritto:
    Hi Tom,
    >
    I just wanted to followup on our conversation. I think I found an
    acceptable (just barely) way of handling my problem with accessing the
    FriendlyName attribute from a base class when no instance of the derived
    class exists.
    >
    What I did was to expose two overloaded methods in the base class, one
    shared and one not. Both of these are essentially the code I've posted
    before. The instance version of the FriendlyName property takes no arguments
    and obtains the Type of the derived class via Me.GetType. The magic of
    polymorphism insures this refers to the derived class. The call for an
    instance of a derived object, which most calls are, is:
    >
    dim name as string = MyDerivedClassI nstance.Friendl yName()
    >
    The shared method requires the Type of the calling object as an argument, as
    e.g.:
    >
    dim name as string = MyDerivedClass. FriendlyName(Ge tType(MyDerived Class))
    >
    Passing the Type of the "calling" class allows the shared method in the base
    class to use relection to obtain the attribute if there is one and process it
    accordingly.
    >
    I object only mildly to the ugliness of the GetType as an argument, and this
    has the distinct advantage of actually working.
    >
    --Jeff

    Comment

    Working...