• Welcome to Valhalla Legends Archive.
 

the Joys of Reflection

Started by KrAzY_NuK, March 30, 2004, 04:04 PM

Previous topic - Next topic

KrAzY_NuK

Has anyone done any Reflection in VB?
I got handed down a testBuilder app that uses Reflection to build test methods to be applied to NUnit.

if i have the following 3 classes:
Public MustInherit Class TestClass1
   Public Function sayHi() As String
       Return "Hello!"
   End Function

   Public MustOverride Function sayBye() As String
End Class


Public Class TestClass1a
   Inherits TestClass1

   Public Overrides Function sayBye() As String
       Return "Bye"
   End Function
End Class


Public Class TestClass1b
   Inherits TestClass1

   Public Overrides Function sayBye() As String
       Return "Good-Bye!"
   End Function
End Class

and i use the following code:
Public Function reflect(Optional ByVal bBuild As Boolean = False) As String
   Dim a As [Assembly] = [Assembly].LoadFrom(mAssemblyNameString)
   Dim mytypes As Type() = a.GetTypes()
   Dim t As Type

   .....

   For Each t In mytypes
       s = Me.buildTestClass(t, bBuild)
   Next t

   .....
end Function

currently in my buildTestClass function i'm calling t.findmembers(...) to get the members of the class that I need. (Which are only public methods).  Is there a way for me to let TestClass1a return "sayHi" and "sayBye" (ie. its members, and the members inherited from TestClass1), but to not return "toString" etc (ie, members inherited from Object).

This is something new for me, and I was handed this code to see if I can do something with it.  Hopefully I'm not just confusing the subject anymore.  Any help would be greatly appreciated!  Thanks

MyndFyre

You could look at the parent classes of the class you're going through, looking to make sure that those classes don't posess the particular member methods that you find in your derived class.

For example -- your buildTestClass class derives from Object and adds methods sayHi and sayBy.  It also contains:


Overridable Function Equals(ByVal o As Object) As Boolean
Overridable Function GetHashCode() As Integer
Overridable Function GetType() As System.Type
Overridable Function ToString() As String


So, your final buildTestClass definition will be:


Sub sayHi()
Sub sayBye()
' The can be marked Overridden
Overridable Function Equals(ByVal o As Object) As Boolean
Overridable Function GetHashCode() As Integer
Overridable Function GetType() As System.Type
Overridable Function ToString() As String


When you reflect over this, look at the members of its parent class (and that parent class, and so on), to see if the members you see in your class are defined.

If they aren't, then you will be good to go.

Also, check the methods, to see if they are overridden in your class.  I'm not sure what you're trying to do, but it's important to watch for.

Cheers.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

KrAzY_NuK

Wow, that confused me to no avail!  lol

the purpose of the buildTestClass function is to create a *.vb file of the class that i can compile to a DLL, and test in NUnit.

I don't want to test Equals, GetHashCode, GetType, or ToString, so I could simply set bindingflags.DeclaredOnly however, i want to test the inherited method sayHi.
I don't want to test the class TestClass1, but I want to test TestClass1a, and TestClass1b.

MyndFyre

Quote from: KrAzY_NuK on March 30, 2004, 06:38 PM
Wow, that confused me to no avail!  lol

the purpose of the buildTestClass function is to create a *.vb file of the class that i can compile to a DLL, and test in NUnit.

I don't want to test Equals, GetHashCode, GetType, or ToString, so I could simply set bindingflags.DeclaredOnly however, i want to test the inherited method sayHi.
I don't want to test the class TestClass1, but I want to test TestClass1a, and TestClass1b.

Let me see if I understand you correctly:

System.Object
    Overridable Function Equals(ByVal o As Object) As Boolean
    Overridable Function GetHashCode() As Integer
    Overridable Function GetType() As System.Type
    Overridable Function ToString() As String
|--> TestClass1
        Overridable Sub sayHi
    |--> TestClass1a
             Overridable Sub sayBye
    |--> TestClass1b
             Overridable Sub sayBye


Is this heirarchy diagram correct?

You want to pick up functions in classes that aren't derived from the mscorlib.dll/System.dll assemblies?

I'm having trouble understanding where you want to go.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

KrAzY_NuK


System.Object
   Overridable Function Equals(ByVal o As Object) As Boolean
   Overridable Function GetHashCode() As Integer
   Overridable Function GetType() As System.Type
   Overridable Function ToString() As String
|--> TestClass1
       Overridable Function sayHi
       MusOverride Function sayBye
   |--> TestClass1a
             Overridable Sub sayBye
   |--> TestClass1b
             Overridable Sub sayBye


Quote
Is this heirarchy diagram correct?

You want to pick up functions in classes that aren't derived from the mscorlib.dll/System.dll assemblies?

That is Correctimundo!
More importantly, i want to pick up functions that ARE derived by the immediate parent class.  That's where I get confused

i.e. I want to pick up:
      Function sayHi()
in TestClass1a
but i don't want to pick up
      Function Equals
or any of the other functions derived elsewhere.
Is this even possible?

MyndFyre

I still think that you would have to iterate over all of the methods, look at the lowest type (always System.Object), and see what assembly they come from.  I know this is definitely a property of a Type.

Might want to look into that. :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

KrAzY_NuK

#6
Alright, thanks!  I'll try that out today!

Edit:
And just to verify, because I don't know much about VB.Net, I was just pushed into it for my co-op work term.  Those 4 functions that I don't want are inherited from System.Object?

MyndFyre

Quote from: KrAzY_NuK on March 31, 2004, 07:13 AM
Alright, thanks!  I'll try that out today!

Edit:
And just to verify, because I don't know much about VB.Net, I was just pushed into it for my co-op work term.  Those 4 functions that I don't want are inherited from System.Object?

Yes.  I recommend doing a Google search for ".NET Reflector" and picking it up -- it's a handy tool for looking at classes and stuff in a little nicer way than ILDasm.  It also has a handy but buggy decompilation feature (it can't handle nested switch statements, which I had in my last bot revision).

But yeah, those four functions derive from System.Object -- because all classes, even Structures, ultimately derive from Object.  :)

Cheers
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

KrAzY_NuK

#8
Wicked awesome!  It works!  LOL

   Private OBJECT_TYPE As System.Type = System.Type.GetType("Object")

......

       Dim mTypes As MemberTypes = MemberTypes.Method
       Dim mFlags As BindingFlags = BindingFlags.Public Or BindingFlags.Static Or BindingFlags.Instance
       Dim mFilter As MemberFilter = Type.FilterAttribute
       Dim mAttr As MethodAttributes = MethodAttributes.Public
       Dim mi As MemberInfo() = t.FindMembers(mTypes, mFlags, mFilter, mAttr)
      Dim m As MethodInfo

.....

         For Each m In mi
               If Not m.GetBaseDefinition.DeclaringType.BaseType Is OBJECT_TYPE Then

......


and etc

Now i just gotta find a way to get rid of "mustoverride" classes, but That shouldn't be too hard

edit: "mustInherit" classes rather, lol