Results 1 to 3 of 3

Thread: Using Reflection to get Properties, Methods, Types

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Using Reflection to get Properties, Methods, Types

    To get all the properties of a class:

    VB Code:
    1. Public Shared Sub GetProperties(ByRef theType As Type)
    2.  
    3.         'To get the properties of an object.
    4.  
    5.         Dim pi_Properties() As PropertyInfo = theType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
    6.  
    7.         'Now, we have the properties.  It's time to loop through them.
    8.  
    9.         Dim pi_PropertyItem As PropertyInfo
    10.  
    11.         For Each pi_PropertyItem In pi_Properties
    12.             Console.WriteLine(pi_PropertyItem.Name & " is of type " & pi_PropertyItem.PropertyType.ToString)
    13.             Console.WriteLine(vbCrLf & pi_PropertyItem.Name & IIf(pi_PropertyItem.CanRead, " can be Read.", " cannot be Read."))
    14.             Console.WriteLine(vbCrLf & pi_PropertyItem.Name & IIf(pi_PropertyItem.CanWrite, " can be Written.", " cannot be Written."))
    15.             Console.WriteLine(vbCrLf & vbCrLf)
    16.         Next
    17.  
    18.     End Sub


    To get all the methods in a class, along with the parameters for each method.

    VB Code:
    1. Public Shared Sub GetMethods(ByRef theType As Type)
    2.         'This is to get the methods of the object.  
    3.  
    4.  
    5.         Dim mi_Properties() As MethodInfo  'array of methodinfo.
    6.         mi_Properties = theType.GetMethods()
    7.  
    8.         Dim mi_MethodItem As MethodInfo
    9.  
    10.         'now, we loop through mi_Properties array and can view each method and its parameters.
    11.  
    12.         For Each mi_MethodItem In mi_Properties
    13.  
    14.             Console.WriteLine(mi_MethodItem.Name & " returns " & mi_MethodItem.ReturnType.ToString)
    15.             Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsPrivate, " is private", " is public."))
    16.             Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsStatic, " is static", " is not static."))
    17.             Console.WriteLine(vbCrLf & mi_MethodItem.Name & IIf(mi_MethodItem.IsConstructor, " is constructor for " & theType.Name.ToString, " is not a constructor"))
    18.             Console.WriteLine(vbCrLf & mi_MethodItem.Name & " takes the following parameters:")
    19.             Console.WriteLine(GetMethodParameters(mi_MethodItem))
    20.             Console.WriteLine(vbCrLf & vbCrLf)
    21.         Next
    22.  
    23.  
    24.     End Sub
    25.  
    26.  
    27. 'This function returns a string and should only be used by GetMethods()
    28.     Private Shared Function GetMethodParameters(ByRef mi As MethodInfo) As String
    29.  
    30.         Dim p_Params() As ParameterInfo
    31.         Dim strParamsText As String = ""
    32.  
    33.         p_Params = mi.GetParameters()
    34.  
    35.         If p_Params.Length = 0 Then
    36.             strParamsText = "***NONE***"
    37.         Else
    38.             Dim pi_ParamItem As ParameterInfo
    39.             For Each pi_ParamItem In p_Params
    40.                 strParamsText &= "  *" & pi_ParamItem.Name & " of type " & pi_ParamItem.ParameterType.ToString & vbCrLf
    41.             Next
    42.         End If
    43.  
    44.         Return strParamsText
    45.  
    46.  
    47.     End Function


    To get all the Types present in the same assembly as a type that you specify:

    VB Code:
    1. Public Shared Sub GetTypes(ByRef theType As Type)
    2.         'every type belongs to an assembly.  Get the assembly OF the type being passed.
    3.  
    4.         Dim TargetAssembly As [Assembly] = theType.Assembly
    5.  
    6.         'Get its namespace
    7.  
    8.         Dim TargetNamespace As String = theType.Namespace
    9.  
    10.         'And now, list out ALL the types in this namespace
    11.  
    12.         Dim TargetTypes() As Type = TargetAssembly.GetTypes
    13.  
    14.         Dim TypeItem As Type
    15.  
    16.         Dim strTypeInfo As String
    17.  
    18.  
    19.         For Each TypeItem In TargetTypes
    20.  
    21.             If TypeItem.Namespace = TargetNamespace Then
    22.                 'This check is performed because an assembly can contain different namespace.  
    23.                 'For example, the namespace for System.Windows.Forms can contain System, System.Resources, etc.
    24.  
    25.                 strTypeInfo = TypeItem.Name & " is a "
    26.  
    27.                 If TypeItem.IsPublic Then
    28.                     strTypeInfo &= "public "
    29.                 Else
    30.                     strTypeInfo &= "private "
    31.                 End If
    32.  
    33.                 If TypeItem.IsClass Then
    34.                     strTypeInfo &= "class."
    35.                 End If
    36.  
    37.                 If TypeItem.IsEnum Then
    38.                     strTypeInfo &= "enum"
    39.                 End If
    40.                 If TypeItem.IsInterface Then
    41.                     strTypeInfo &= "interface"
    42.                 End If
    43.                 Console.WriteLine(strTypeInfo)
    44.  
    45.             End If
    46.  
    47.  
    48.  
    49.         Next
    50.  
    51.  
    52.     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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Good work Special Agent Mendhak.
    I don't live here any more.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by wossname
    Special Agent Mendhak.
    SAM I am

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width