J
Jeff Mason
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
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