Valhalla Legends Forums Archive | .NET Platform | the Joys of Reflection

AuthorMessageTime
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
March 30, 2004, 10:04 PM
Myndfyr
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:

[code]
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
[/code]

So, your final buildTestClass definition will be:

[code]
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
[/code]

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.
March 30, 2004, 11:56 PM
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.
March 31, 2004, 12:38 AM
Myndfyr
[quote author=KrAzY_NuK link=board=37;threadid=6076;start=0#msg52764 date=1080693526]
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.
[/quote]

Let me see if I understand you correctly:
[code]
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
[/code]

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.
March 31, 2004, 1:53 AM
KrAzY_NuK
[code]
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
[/code]

[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?
[/quote]

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?
March 31, 2004, 12:48 PM
Myndfyr
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. :)
March 31, 2004, 12:56 PM
KrAzY_NuK
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?
March 31, 2004, 1:13 PM
Myndfyr
[quote author=KrAzY_NuK link=board=37;threadid=6076;start=0#msg52824 date=1080738834]
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?
[/quote]

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
March 31, 2004, 8:00 PM
KrAzY_NuK
Wicked awesome! It works! LOL

[code] 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

......[/code]

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
April 1, 2004, 2:42 PM

Search