Custom Attributes, Shared methods, Derived classes and reflection



Hi,

I'm having a reflection brain fog here, perhaps someone can set me on the right
track.

I'd like to define a custom attribute to be used in a class hierarchy.

What I want to do is to have an attribute which can be applied to a class definition
of a class which inherits from a base, mustinherit class. I want to define methods
in the base class which will access the contents of the attribute as it is applied to
a particular derived class.

This was reasonably easy to do when dealing with an instance of the derived class.
But, I need to be able to have these methods in the base class be shared, so the
attribute can be accessed as defined on the derived class, rather than on an instance
of the class.

That is, for the instance method I define a derived class with my custom attribute
as, say:

<MyCustomAttribute("String1", "String2")> _
Public MyDerivedClass
Inherits MyBaseClass
...

Then, in the base class, I defined a method like:

Public ReadOnly Property ValueFromMyAttribute() as string
Get
Dim classtype As Type = Me.GetType
Dim attr As EntityAttribute =
DirectCast(Attribute.GetCustomAttribute(classtype, _
GetType(MyCustomAttribute)), MyCustomAttribute)
If attr IsNot Nothing Then
Return attr.PropertyForString1
Else
Return classtype.Name
End If
End Get
End Property

This seemed to work.

But, the requirement now exists to access the attribute's properties from SHARED
methods, since the values may be needed even if no instance of the derived class
exists.

It seems like this ought to be possible, but how? If I make the property shared,
then obviously there is no instance for "Me" to refer to, so this generates a compile
error. How can I get the type of the derived CLASS instead of from an instance of
that class? I assume if I can get that, I can track down the attribute and access
its properties as I did above.

Thanks,

-- Jeff
.



Relevant Pages

  • Re: Application.Run() problem
    ... still am very much confused about the paint event handler. ... It most obviously should be the derived class, ... It seems that you're not understanding how inheritance works. ... which ensures that the base class uses the same ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Very Confused on Page 33
    ... On page 33 Bruce is explaining inheritance. ... > base class you can also send to objects of the derived class. ... The programmatic difference enables the subclass ...
    (comp.lang.java.help)
  • Re: attribute or subclass
    ... With inheritance the derived class code ... reference in place of the base class reference. ...
    (comp.object)
  • Re: C# inheritance broken?
    ... class as a derived class, you can do the opposite though. ... Ignacio Machin ... issue of inheriting a base class ... You can't use a base class instance as though it were a derived class ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# inheritance broken?
    ... class as a derived class, you can do the opposite though. ... Ignacio Machin ... and providing a method for the base class object to become the object at ... You can't use a base class instance as though it were a derived class ...
    (microsoft.public.dotnet.languages.csharp)

Loading