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



On 31 Jul 2006 08:32:37 -0700, tommaso.gastaldi@xxxxxxxxxxx wrote:

I anticipated your observation by 2 using 2 classes:

MyDerivedClass_Instance
MyDerivedClass

both display correct attributes, although the same type is used in the
gettype.
Can you make an example where this scheme would fail, so that we can
fix it ?

I really appreciate your help and interest here.

Let's concentrate on the issue where no class instance is involved, and the shared
method on the base class is invoked through the derived class (since that's were my
problem lies).

Suppose I define two classes both decorated with their own version of the attribute:

<MyCustomAttribute("Default1", "Default2")> _
Public Class MyDerivedClass
Inherits MyBaseClass

End Class

<MyCustomAttribute("Hi", "Tom")> _
Public Class AnotherDerivedClass
Inherits MyBaseClass

End Class

How would the assignment of the 'classtype' variable in the base's shared method be
made? As you originally suggested, it was:

Dim classtype As Type = GetType(MyDerivedClass)

but now I have two classes, MyDerivedClass AND AnotherDerivedClass, both of which
derive from MyBaseClass, and for both of which I'd like to evaluate each's attribute,
as, say:

Dim x as string = MyDerivedClass.S_ValueFromMyAttribute(1)
Dim y as string = AnotherDerivedClass.S_ValueFromMyAttribute(1)

I would hope to return x = "Default1" and y = "Hi".

It seems to me that GetType won't work here as it needs an explicit Type as its
argument. Since there can be multiple classes that inherit from the base, I don't
know that Type in the base class, as it is only known at run-time when the shared
method is invoked.

I'm thinking that some other form of reflection is required to get the Type of the
derived classin the base's shared method. I have no idea what that might be...

-- Jeff
.



Relevant Pages

  • Re: Type Conversion with Date formatted as YYYYMMDD
    ... Dim y as Date = CDATE ... I guess you'll be glad to know that the Date class has a Shared method ... should have figured there'd be a Framework method for handling it. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: String to Byte & Vice Versa
    ... shared method means you don't use an instance, you use a class name:) Although, in VB.Net you can use instance variables to call shared methods.. ... Dim instance As System.Text.Encoding ... Dim returnValue As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to write a service program that can call and run a script?
    ... > Dim p As New Process ... Always remember that 'Start' is a shared method of the 'Process' class. ... Herfried K. Wagner [MVP] ...
    (microsoft.public.dotnet.languages.vb)