devxlogo

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

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())'    NextFunction 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 htEnd Function

devx-admin

Share the Post: