1 Replies - 781 Views - Last Post: 31 July 2011 - 11:44 AM Rate Topic: -----

#1 gwolf2u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-June 11

[Help] System.Reflection - dinamic load dlls

Posted 31 July 2011 - 11:01 AM

hi there all
so I want to go start using System.Reflection to load my dlls from a special folder as I never did this before and thought I should give it a try. now I'm wondering how to do this.
below I have my dll code and my form code
I need to know how to use the sub from my dll in my form if I use Assembly.LoadFrom

mydll
Public Class Class1
    Public Sub msg(ByVal message As String)
        MsgBox(message)
    End Sub
End Class


my form
Option Strict On
Imports System.IO
Imports System.Reflection

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String = Application.StartupPath & "/dlls/mydll.dll"

        If File.Exists(path) Then
            Dim ass As Assembly = Assembly.LoadFrom(path)
            'any ideas here
        End If
    End Sub

End Class



hope you get my idea and you are willing to help out

Is This A Good Question/Topic? 0
  • +

Replies To: [Help] System.Reflection - dinamic load dlls

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • Posts: 969
  • Joined: 30-September 10

Re: [Help] System.Reflection - dinamic load dlls

Posted 31 July 2011 - 11:44 AM

Hi,

There are numerous superficially different ways you could do this, but they all ultimately follow a similar process.

1) You have the Assembly object representing your .dll, returned from the Assembly.LoadFrom() method. This is fine.

2) Next, you need to create an instance of Class1. One way of doing this is calling the CreateInstance() method on the Assembly object representing your .dll. You pass in the fully qualified name of Class1 into CreateInstance(), so it knows what class to create an instance of.

For example, if the namespace the class is in is called 'mydll', and the class name is 'Class1', the fully qualified name would be "mydll.Class1". That is what you would pass into CreateInstance().

CreateInstance() returns an object of type Object, which you should store in a variable. This is an instance of Class1.

3) Now, in order to invoke the members of Class1 (you cannot do so directly as it is of type Object, and therefore effectively only has the members of the Object class available), you need to get the Type object for the instance, which you can get using the GetType() method of the Object type. You can call this on the instance returned by CreateInstance(). Store the returned Type object in a variable.

4) Now, you can call InvokeMember() on that Type object, passing in the name of the method to call ("msg"), a BindingFlags value (BindingFlags.InvokeMethod, in this case), a Binder object (which can be Nothing, in this case), the target object on which to invoke the method (the instance object returned from CreateInstance()) and any arguments you want passing to the method, as an array of Object types (which will be the message you want to display, in this case).


And that should invoke the method dynamically :)

This post has been edited by CodingSup3rnatur@l-360: 31 July 2011 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1