To get all the properties of a class:
VB Code:
Public Shared Sub GetProperties(ByRef theType As Type) 'To get the properties of an object. Dim pi_Properties() As PropertyInfo = theType.GetProperties((BindingFlags.Public Or BindingFlags.Instance)) 'Now, we have the properties. It's time to loop through them. Dim pi_PropertyItem As PropertyInfo For Each pi_PropertyItem In pi_Properties Console.WriteLine(pi_PropertyItem.Name & " is of type " & pi_PropertyItem.PropertyType.ToString) Console.WriteLine(vbCrLf & pi_PropertyItem.Name & IIf(pi_PropertyItem.CanRead, " can be Read.", " cannot be Read.")) Console.WriteLine(vbCrLf & pi_PropertyItem.Name & IIf(pi_PropertyItem.CanWrite, " can be Written.", " cannot be Written.")) Console.WriteLine(vbCrLf & vbCrLf) Next End Sub
To get all the methods in a class, along with the parameters for each method.
VB Code:
Public Shared Sub GetMethods(ByRef theType As Type) 'This is to get the methods of the object. Dim mi_Properties() As MethodInfo 'array of methodinfo. mi_Properties = theType.GetMethods() Dim mi_MethodItem As MethodInfo 'now, we loop through mi_Properties array and can view each method and its parameters. For Each mi_MethodItem In mi_Properties Console.WriteLine(mi_MethodItem.Name & " returns " & mi_MethodItem.ReturnType.ToString) Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsPrivate, " is private", " is public.")) Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsStatic, " is static", " is not static.")) Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsConstructor, " is constructor for " & theType.Name.ToString, " is not a constructor")) Console.WriteLine(vbCrLf & mi_MethodItem.Name & " takes the following parameters:") Console.WriteLine(GetMethodParameters(mi_MethodItem)) Console.WriteLine(vbCrLf & vbCrLf) Next End Sub 'This function returns a string and should only be used by GetMethods() Private Shared Function GetMethodParameters(ByRef mi As MethodInfo) As String Dim p_Params() As ParameterInfo Dim strParamsText As String = "" p_Params = mi.GetParameters() If p_Params.Length = 0 Then strParamsText = "***NONE***" Else Dim pi_ParamItem As ParameterInfo For Each pi_ParamItem In p_Params strParamsText &= " *" & pi_ParamItem.Name & " of type " & pi_ParamItem.ParameterType.ToString & vbCrLf Next End If Return strParamsText End Function
To get all the Types present in the same assembly as a type that you specify:
VB Code:
Public Shared Sub GetTypes(ByRef theType As Type) 'every type belongs to an assembly. Get the assembly OF the type being passed. Dim TargetAssembly As [Assembly] = theType.Assembly 'Get its namespace Dim TargetNamespace As String = theType.Namespace 'And now, list out ALL the types in this namespace Dim TargetTypes() As Type = TargetAssembly.GetTypes Dim TypeItem As Type Dim strTypeInfo As String For Each TypeItem In TargetTypes If TypeItem.Namespace = TargetNamespace Then 'This check is performed because an assembly can contain different namespace. 'For example, the namespace for System.Windows.Forms can contain System, System.Resources, etc. strTypeInfo = TypeItem.Name & " is a " If TypeItem.IsPublic Then strTypeInfo &= "public " Else strTypeInfo &= "private " End If If TypeItem.IsClass Then strTypeInfo &= "class." End If If TypeItem.IsEnum Then strTypeInfo &= "enum" End If If TypeItem.IsInterface Then strTypeInfo &= "interface" End If Console.WriteLine(strTypeInfo) End If Next End Sub
Although they're all being written to console, it's just for the purpose of displaying the output. Modify it as you see fit.
Keywords:
System.Reflection
Method
Method Parameters
Properties
Types
Assembly
Mendhak![]()




Reply With Quote