Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: Intermediate
Jan 5, 2004

GetAllMembers - Retrieving a hashtable containing all the members in a type

' Return a case-insensitive hashtable containing all the members in a type
' Note: requires Imports System.Reflection
'
' Example:
'    Dim ht As Hashtable = GetAllMembers(GetType(String), False)
'    For Each key As Object In ht.Keys
'        Dim member As MemberInfo = DirectCast(ht(key), MemberInfo)
'        Debug.WriteLine(key.ToString() & " - " & '           
' member.MemberType.ToString())
'    Next

Function GetAllMembers(ByVal type As Type, ByVal includePrivate As Boolean) As _
    Hashtable
    Dim ht As Hashtable = _
        Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable()
    ' decide which members should be retrieved
    Dim flags As BindingFlags = BindingFlags.Instance Or BindingFlags.Static Or _
        BindingFlags.Public
    If includePrivate Then
        flags = flags Or BindingFlags.NonPublic
    End If
    ' move members into the hashtable
    For Each member As MemberInfo In type.GetMembers(flags)
        ' avoid duplicates caused from member overloading
        If Not ht.Contains(member.Name) Then
            ' filter off special methods
            If member.MemberType <> MemberTypes.Method OrElse CType(member, _
                MethodInfo).IsSpecialName = False Then
                ht.Add(member.Name, member)
            End If
        End If
    Next
    Return ht
End Function
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap