Reflection problem

  • Thread starter Thread starter Marco
  • Start date Start date
M

Marco

Hi,
I try to use reflection to retrieve class B members and not inherited
members. I wonder why the myMember value is nothing. Any suggestions?

Thanks


Module Module1

Sub Main()
Dim myClass As B = New B
Dim myMembers As System.Reflection.MemberInfo() =
myClass.GetType.GetMembers(Reflection.BindingFlags.DeclaredOnly)

End Sub

End Module

Class A
Public a1 As Integer
Public a2 As Integer
End Class

Class B
Inherits A

Public b1 As Integer
End Class
 
Read the Note on this page in the MSDN Documentation:
http://msdn.microsoft.com/library/d...lrfsystemreflectionbindingflagsclasstopic.asp

<quote>
You must specify Instance or Static along with Public or NonPublic or no
members will be returned.
</quote>

So your call should look like:
Dim myMembers As System.Reflection.MemberInfo() = _
myClass.GetType.GetMembers( _
Reflection.BindingFlags.Public _
Or Reflection.BindingFlags.Instance _
Or Reflection.BindingFlags.DeclaredOnly)

hope that helps..
Imran.
 
Marco,
Because "BindingFlags.DeclaredOnly" is "used to change how the search
works", which means you need to include other BindingFlags to actually
define the search, such as BindingFlags.Public to include public members,
also you need at least BindingFlags.Instance or BindingFlags.Static to say
if you want instance members or include Shared members also...

Try something like:
Dim flags As BindingFlags = BindingFlags.Instance _
Or BindingFlags.Public _
Or BindingFlags.DeclaredOnly
Dim myMembers As System.Reflection.MemberInfo()
=myClass.GetType.GetMembers(flags)

For details see:
http://msdn.microsoft.com/library/d...html/frlrfSystemTypeClassGetMembersTopic2.asp

Hope this helps
Jay
 
Back
Top