Imports Microsoft.VisualBasic Imports System Imports System.Collections Imports System.IO Imports System.Reflection Imports System.Text Class FindType Private Const SHOW_INTERFACES As Integer = 1 Private Const SHOW_FIELDS As Integer = 2 Private Const SHOW_PROPERTIES As Integer = 4 Private Const SHOW_EVENTS As Integer = 8 Private Const SHOW_METHODS As Integer = 16 Private Const SHOW_MODULE_INFO As Integer = 32 Private myVerboseWriter As IndentedWriter = New IndentedWriter() Private myWriter As IndentedWriter = New IndentedWriter() Private exactMatchOnly As Boolean = False Private recurseTypes As Boolean = False Private matchOnlyNamespace As Boolean = False Private wideSearch As Boolean = False Private showOptions As Integer = 0 Private myClassList As ArrayList = New ArrayList() Private DirList As ArrayList = New ArrayList() Public Property ExactMatchOnly() As Boolean Get Return exactMatchOnly End Get Set(value As Boolean) exactMatchOnly = value End Set End Property Public Property RecurseTypes() As Boolean Get Return recurseTypes End Get Set(value As Boolean) recurseTypes = value End Set End Property Public Property MatchOnlyNamespace() As Boolean Get Return matchOnlyNamespace End Get Set(value As Boolean) matchOnlyNamespace = value End Set End Property Public Property WideSearch() As Boolean Get Return wideSearch End Get Set(value As Boolean) wideSearch = value End Set End Property Public Property VerbosePrint() As Boolean Get Return myVerboseWriter.Print End Get Set(value As Boolean) myVerboseWriter.Print = value End Set End Property Public Property ShowInterfaces() As Boolean Get Return (showOptions And 1) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 1 Else showOptions = showOptions And -2 End If End Set End Property Public Property ShowFields() As Boolean Get Return (showOptions And 2) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 2 Else showOptions = showOptions And -3 End If End Set End Property Public Property ShowProperties() As Boolean Get Return (showOptions And 4) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 4 Else showOptions = showOptions And -5 End If End Set End Property Public Property ShowEvents() As Boolean Get Return (showOptions And 8) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 8 Else showOptions = showOptions And -9 End If End Set End Property Public Property ShowMethods() As Boolean Get Return (showOptions And 16) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 16 Else showOptions = showOptions And -17 End If End Set End Property Public Property ShowModuleInfo() As Boolean Get Return (showOptions And 32) = 0 = False End Get Set(value As Boolean) If value Then showOptions = showOptions Or 32 Else showOptions = showOptions And -33 End If End Set End Property Public Sub Search() Dim i As Integer If myClassList.Count <> 0 Then For i = 0 To myClassList.Count - 1 Search(CType(myClassList(i), String)) Next i End If End Sub Public Sub Search(theSearchString As String) Dim j As Integer Dim i As Integer Dim j2 As Integer Dim testAssy As [Assembly] = [Assembly].Load("mscorlib.dll") myVerboseWriter.WriteLine("Searching System Libraries") Dim dirFrameworks As String = Path.GetDirectoryName(testAssy.Location) Dim l As ArrayList = New ArrayList() BuildDLLFileList(dirFrameworks, l) For j = 0 To l.Count - 1 Search(theSearchString, CType(l(j), String)) Next j myVerboseWriter.WriteLine("Searching the current directory...") l = New ArrayList() BuildDLLFileList(".", l) For i = 0 To l.Count - 1 Search(theSearchString, CType(l(i), String)) Next i Dim dir As Object() = DirList.ToArray() For j2 = 0 To CInt(dir.Length) - 1 Dim k As Integer myVerboseWriter.WriteLine("Searching directory {0}...", New Object(){dir(j2)}) l = New ArrayList() BuildDLLFileList(CType(dir(j2), String), l) For k = 0 To l.Count - 1 Search(theSearchString, CType(l(k), String)) Next k Next j2 End Sub Public Sub Search(theSearchString As String, theModule As String) Try Dim j As Integer Dim a As [Assembly] = [Assembly].LoadFrom(theModule) Dim m As [Module]() = a.GetModules() theSearchString = theSearchString.ToUpper() For j = 0 To CInt(m.Length) - 1 myVerboseWriter.WriteLine("Searching Module {0}", New Object(){theModule}) If Not m Is Nothing Then Dim i As Integer Dim types As Type() = m(j).GetTypes() For i = 0 To CInt(types.Length) - 1 Dim curType As Type = types(i) Dim name As String = curType.FullName.ToUpper() If ExactMatchOnly Then If name = theSearchString Then DumpType(curType) End If ElseIf MatchOnlyNamespace Then If Not curType.Namespace Is Nothing AndAlso curType.Namespace.ToUpper() = theSearchString Then DumpType(curType) End If ElseIf WideSearch Then If curType.Namespace.ToUpper().IndexOf(theSearchString) <> -1 Then DumpType(curType) End If ElseIf name = theSearchString Then Dim oldOptions As Integer = showOptions If showOptions = 0 Then ShowAll() End If DumpType(curType) showOptions = oldOptions ElseIf name.IndexOf(theSearchString) <> -1 Then DumpType(curType) End If Next i End If Next j Catch rcle As ReflectionTypeLoadException Dim i2 As Integer Dim loadedTypes As Type() = rcle.Types Dim exceptions As Exception() = rcle.LoaderExceptions Dim exceptionCount As Integer = 0 For i2 = 0 To CInt(loadedTypes.Length) - 1 If loadedTypes(i2) Is Nothing Then exceptionCount += 1 End If Next i2 Catch fnfe As FileNotFoundException myVerboseWriter.WriteLine(fnfe.Message) Catch End Try End Sub Public Sub ShowAll() ShowInterfaces = True ShowMethods = True ShowFields = True ShowProperties = True ShowEvents = True ShowModuleInfo = True End Sub Public Sub DirAdd(dir As String) DirList.Add(dir) End Sub Public Sub ClassAdd(newClass As String) myClassList.Add(newClass) End Sub Private Sub BuildDLLFileList(directory As String, list As ArrayList) Try If Directory.Exists(directory) Then Dim i As Integer Dim e As String() = Directory.GetFiles(directory, "*.dll") For i = 0 To CInt(e.Length) - 1 list.Add(e(i)) Next i Else myVerboseWriter.WriteLine("Directory [{0}] does not exist!", New Object(){directory}) End If Catch Ex As Exception If Not Ex.[GetType]() Is GetType(InvalidOperationException) Then End If End Try End Sub Private Function GetTypeDescription(aType As Type) As String Dim str As String = Nothing If aType.IsClass Then str = "class" End If If aType.IsInterface Then str = "interface" End If If aType.IsValueType Then str = "struct" End If If aType.IsArray Then str = "array" End If Dim str2 As String = str Return str2 End Function Private Sub DumpType(aType As Type) Dim baseType As Type = aType.BaseType myWriter.WriteLine("{0,-10} {1}", New Object(){GetTypeDescription(aType), aType}) If ShowModuleInfo Then myWriter.WriteLine("{0,-10} {1}", New Object(){"Module:", aType.Module.FullyQualifiedName}) End If DumpInterfaces(aType) DumpFields(aType) DumpProperties(aType) DumpEvents(aType) DumpMethods(aType) If RecurseTypes Then myWriter.WriteLine() End If If RecurseTypes AndAlso Not baseType Is Nothing Then myWriter.PushIndent() myVerboseWriter.PushIndent() DumpType(baseType) myWriter.PopIndent() myVerboseWriter.PopIndent() End If End Sub Private Sub DumpInterfaces(aType As Type) If ShowInterfaces Then Dim info As Type() = aType.GetInterfaces() If CInt(info.Length) <> 0 Then Dim i As Integer myWriter.WriteLine("{0} {1}", New Object(){"# Interfaces:", CInt(info.Length)}) For i = 0 To CInt(info.Length) - 1 myWriter.PushIndent() myWriter.WriteLine("interface {0}", New Object(){info(i).FullName}) If ShowMethods Then myWriter.PushIndent() DumpType(info(i)) myWriter.PopIndent() End If myWriter.PopIndent() Next i End If End If End Sub Private Sub DumpProperties(aType As Type) If ShowProperties Then Dim pInfo As PropertyInfo() = aType.GetProperties() myWriter.WriteLine("Properties") Dim found As Boolean = False If CInt(pInfo.Length) <> 0 Then Dim i As Integer Dim curInfo As PropertyInfo = Nothing For i = 0 To CInt(pInfo.Length) - 1 curInfo = pInfo(i) If curInfo.DeclaringType Is aType Then found = True Dim flags As String = Nothing If curInfo.CanRead AndAlso curInfo.CanWrite Then flags = "get; set;" ElseIf curInfo.CanRead Then flags = "get" ElseIf curInfo.CanWrite Then flags = "set" End If myWriter.WriteLine(" {0,-10} '{1}' ", New Object(){curInfo, flags}) End If Next i End If If Not found Then myWriter.WriteLine(" (none)") End If End If End Sub Private Sub DumpEvents(aType As Type) If ShowEvents Then Dim i As Integer Dim eInfo As EventInfo() = aType.GetEvents() myWriter.WriteLine("Events:") Dim found As Boolean = False If CInt(eInfo.Length) <> 0 Then For i = 0 To CInt(eInfo.Length) - 1 If eInfo(i).DeclaringType Is aType Then found = True myWriter.WriteLine(" {0}", New Object(){eInfo(i)}) End If Next i End If If Not found Then myWriter.WriteLine(" (none)") End If End If End Sub Private Sub DumpFields(aType As Type) If ShowFields Then Dim i As Integer Dim info As FieldInfo() = aType.GetFields() myWriter.WriteLine("Fields:") Dim found As Boolean = False If CInt(info.Length) <> 0 Then For i = 0 To CInt(info.Length) - 1 If info(i).DeclaringType Is aType Then myWriter.WriteLine(" {0}", New Object(){info(i)}) found = True End If Next i End If If Not found Then myWriter.WriteLine(" (none)") End If End If End Sub Private Sub DumpMethods(aType As Type) If ShowMethods Then Dim i As Integer Dim mInfo As MethodInfo() = aType.GetMethods() myWriter.WriteLine("Methods") Dim found As Boolean = False If CInt(mInfo.Length) <> 0 Then For i = 0 To CInt(mInfo.Length) - 1 If mInfo(i).DeclaringType Is aType AndAlso Not mInfo(i).IsSpecialName Then found = True Dim modifiers As StringBuilder = New StringBuilder() If mInfo(i).IsStatic Then modifiers.Append("static ") End If If mInfo(i).IsPublic Then modifiers.Append("public ") End If If mInfo(i).IsFamily Then modifiers.Append("protected ") End If If mInfo(i).IsAssembly Then modifiers.Append("internal ") End If If mInfo(i).IsPrivate Then modifiers.Append("private ") End If myWriter.WriteLine(" {0} {1}", New Object(){modifiers, mInfo(i)}) End If Next i End If If Not found Then myWriter.WriteLine(" (none)") End If End If End Sub End Class