Results 1 to 10 of 10

Thread: How to retrieve the methods from a dll?

  1. #1

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66

    How to retrieve the methods from a dll?

    How to retrieve the methods from a dll?

    Can I retrieve them using the System.Reflection.Assembly? How?

    Thanks!

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yep
    VB Code:
    1. Imports System
    2. Imports System.Reflection
    3.  
    4. Public Module DLLReport
    5.     Public Sub Main(ByVal args() As String)
    6.         LoadDLL(args(0))
    7.    
    8.     End Sub
    9.    
    10.     Public Sub LoadDLL(ByVal filename As String)
    11.         Dim asm As [Assembly]     ' Assembly is reserved vb word so use the brackets to override
    12.         Dim myClasses() As Type
    13.         Dim myMethods() As MethodInfo
    14.         Dim myEvents() As EventInfo
    15.         Dim myProperties() As PropertyInfo
    16.         Dim myParameters() As ParameterInfo
    17.        
    18.         Dim myTypes As Type
    19.         Dim myMethodInfo As MethodInfo
    20.         Dim myEventInfo As EventInfo
    21.         Dim myPropertyInfo As PropertyInfo
    22.         Dim myParameterInfo As ParameterInfo
    23.        
    24.         asm = [Assembly].LoadFrom(filename)
    25.         If Not(asm Is Nothing) Then
    26.             myClasses = asm.GetTypes()
    27.         End If
    28.        
    29.         For Each myTypes In myClasses
    30.             ' Output Classname
    31.             Console.Writeline(myTypes.ToString())
    32.            
    33.             ' Lets get all methods(sub and function) from this class
    34.             myMethods = myTypes.GetMethods()
    35.             For Each myMethodInfo In myMethods
    36.                 Console.Writeline("---Method---" & myMethodInfo.Name & " Returns " & myMethodInfo.ReturnType.ToString())
    37.                
    38.                 ' Now lets get the Parameters for this method
    39.                 myParameters = myMethodInfo.GetParameters()
    40.                 For Each myParameterInfo In myParameters
    41.                     Console.WriteLine("    ---Parameter---" & myParameterInfo.Name & " As " & myParameterInfo.ParameterType.ToString())
    42.                 Next myParameterInfo
    43.             Next myMethodInfo
    44.            
    45.             ' Lets get all events
    46.             myEvents = myTypes.GetEvents()
    47.             For Each myEventInfo In myEvents
    48.                 Console.Writeline("---Event---" & myEventInfo.Name)
    49.             Next myEventInfo
    50.            
    51.             ' Lets get all properties
    52.             myProperties = myTypes.GetProperties()         
    53.             For Each myPropertyInfo In myProperties
    54.                 Console.Writeline("---Properties---" & myPropertyInfo.Name & " As " & myPropertyInfo.PropertyType.ToString())
    55.             Next myPropertyInfo
    56.         Next myTypes
    57.     End Sub
    58. End Module
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    What an excellent reply! Thank you very much, Cander!

  4. #4

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    BTW, is it possible to know the security settings of the methods declared with the [PrincipalPermissionAttribute()] ?

    Thanks!

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    do you mean check if its public,private, friend, etc?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    No this is about the System.Security of the .Net framework

    I need to know wich username or role is the method expecting. Or, at least, to know if my Principal has the permissions that allow-me to execute the method safely (without having to call the method explicitly in search for an exception).

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well I havnt really worked with Attributes much yet so I cant be 100%, but you may try looking into the MethodInfo objects Attributes property. Maybe that will provide some clue as since I dont know much about using Attributes, I wouldnt really be able to piece together what to do.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    Well, I think that's not the way. At least I didn't found nothing...

    I've made the question about reading the assembly methods on the c# forum. In fact, I've converted your code to c#. Do you mind if I publish that code with a reference to you and this thread on that forum?

    Thanks!

  9. #9
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    search by my posts and reflection...cander some time ago posted a version of that code in C#
    \m/\m/

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Actually I dont think I did this code in C#. I did the plug-in code orginally in C# and Edneeis ported that to VB.NET

    But yeah hd..that is fine with me.
    Last edited by Cander; Jan 29th, 2003 at 09:41 AM.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

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