Code://************************************** // // Name: Use Reflection to list all Colo // r objects // Description:This is a good example in // the power and flexibility of using Refle // ction. This example will display in a co // nsole a complete list of all the Colors // available in the System.Drawing.Color na // mespace. Put into a file colors.vb and c // ompile at the command line using 'vbc co // lors.vb' then run the colors.exe // By: Chris Andersen // //This code is copyrighted and has // limited warranties.Please see http:// // www.Planet-Source-Code.com/xq/ASP/txtCod // eId.482/lngWId.10/qx/vb/scripts/ShowCode // .htm //for details. //************************************** // Imports System Imports System.Reflection Public Module ListColors Public Sub Main(ByVal args() As String) GetColors() End Sub Public Sub GetColors() Dim asm As [Assembly] ' Assembly is reserved vb word so use the brackets to override Dim myClasses() As Type Dim myProperties() As PropertyInfo Dim myTypeInfo As Type Dim myPropertyInfo As PropertyInfo ' Lets load the System.Drawing dll asm = [Assembly].LoadWithPartialName("System.Drawing") If Not(asm Is Nothing) Then ' get an array of base classes myClasses = asm.GetTypes() End If ' Loop through each class For Each myTypeInfo In myClasses ' if it is the color class If myTypeInfo.ToString() = "System.Drawing.Color" Then ' get an array of all the properties myProperties = myTypeInfo.GetProperties() ' loop the properties For Each myPropertyInfo In myProperties ' if the property returns a Color object, then lets output it ' to the console as their are some non color properties we ' dont want to list If myPropertyInfo.PropertyType.ToString() = "System.Drawing.Color" Then Console.Writeline(myPropertyInfo.Name) End If Next myPropertyInfo End If Next myTypeInfo End Sub End Module




Reply With Quote