Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add reference to Class Library at runtime. 3

Status
Not open for further replies.

Disferente

Programmer
Jun 23, 2008
112
0
0
US
How do I use a class library dll that I don't add a reference at design time. I only want to use it if it is selected in the program.
 
Here is an example. I have created a Class Library called SimpleClassLibrary. I've got a single class -- Class1 which contains a single, shared method -- DoSomething. DoSomething simply displays a message box. Here is the code for my class library
Code:
Public Class Class1
    Shared Sub DoSomething()
        System.Windows.Forms.MessageBox.Show("hello from SimpleClassLibrary")
    End Sub
End Class

Here is the code for the calling application. When the button is pressed, we should get a messagebox from the .dll.

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim scl As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("c:\SimpleClassLibrary.dll")
        Dim t As Type = SCL.GetType("SimpleClassLibrary.Class1")
        t.InvokeMember("DoSomething", Reflection.BindingFlags.InvokeMethod, Nothing, Nothing, Nothing)
    End Sub
 
Looks like it could be what I need, but I can't get it to work as I need.

Is there any way of invoking non shared members?
 
Try this:

Code:
Public Class Class1
    Shared Sub DoSomething()
        System.Windows.Forms.MessageBox.Show("hello from SimpleClassLibrary")
    End Sub
[b]
    Sub DoSomethingElse()
        System.Windows.Forms.MessageBox.Show("hello from SimpleClassLibrary -- instance")
    End Sub[/b]
End Class

Code:
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim scl As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("c:\SimpleClassLibrary.dll")
        Dim t As Type = SCL.GetType("SimpleClassLibrary.Class1")
        t.InvokeMember("DoSomething", Reflection.BindingFlags.InvokeMethod, Nothing, Nothing, Nothing)
    End Sub
[b]
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim scl As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("c:\SimpleClassLibrary.dll")
        Dim t As Type = scl.GetType("SimpleClassLibrary.Class1")
        Dim first_instance As Object = scl.CreateInstance("SimpleClassLibrary.Class1")
        t.InvokeMember("DoSomethingElse", Reflection.BindingFlags.InvokeMethod, Nothing, first_instance, Nothing, Nothing)
    End Sub[/b]
End Class
 
I changed a few things but now it works great.
I am not using InvokeMember at all and I removed the Dim t as type.

Public ClassO as object

Dim scl As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("C:\MyDll.dll")
Dim obj(1) As Object
obj(0) = "Software\\Something\\Other"
obj(1) = SomeClass
ClassO = scl.CreateInstance("MyDll.Class1", False, Reflection.BindingFlags.InvokeMethod, Nothing, obj, Nothing, Nothing)

Thank you very much for your help.
I use any public variables/procedures/fuctions by
Something=ClassO.Var1
ClassO.SomeSub()


Only problem I have is that I can't put
AddressOf SomeFunction
as a argument to the constructor, but I made a couple of small changes to the Class so I don't really need it anymore.
 
River- Nicely done.

To expand on this idea:

If the DLL had a form(s). The parent application is MDI. Is it possible to make the DLL Form(s) a child of the MDI?

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
I don't see why not, the dll I have and use creates tabpages on an existing tabcontrol. I think that would be somewhat similar to MDI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top