Dynamically loading an assembly? 
Author Message
 Dynamically loading an assembly?

I have developed a proto-type application that supports a plug in
architecture.

I have a simple interface defined in an assembly that has a few methods,
nothing else.

I then created a Class Library with a simple class that implemented that
interface.

Finally, I have a simple host application that uses the interface to load
the Class Library.  My question is that the host app has a direct
reference to the Class Library.  What I want to do is be able to
dynamically create an object of the Class Library without having to
directly reference the .dll.  

For Example:

'*** BEGIN CODE (UNTESTED) HERE ****

'MyInterface.vb
'Inteface defined in an assembly
Public Interface IMyInterface
        Sub SomeMethod()
End Interface

'MyClass.vb
'Class that implements the interface
Public Class MyInterfaceClass
        Implements MyInterface.IMyInterface

        Public Sub SomeMethod()
                'Implementation of SomeMethod here
        End Sub
End Class

'MyHost.vb
'Host that uses the class library
Public Class frmHost
        Inherits System.Windows.Forms.Form

        '**** This line is too specific.  I need it to be more generic
        Private oInterfaceObject as MyInterface.IMyInterface            '<<<<<<<

        Private Sub Button1_Click('blah blah here)
                oInterfaceObject = New MyClass.MyInterfaceClass
                'Use objec here
        End Sub
End Class

'*** END CODE ***

The line in the button click specifically creates an instance of the
class defined in the class library.  I need this to be more generic as
there will be other Class Libraries that implement the interface.  I can
setup a folder that all plugins are copied into and have the app read the
file in that folder and load them, but how can I create an instance of
the appropriate plugin when I need to?  For example, the user clicks on a
menu item that has an entry for each plug in.  This is the part I not
crystal clear on.  

Any help would be apreciated.

Thanks again,

Chris
--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail



Sat, 28 May 2005 04:06:42 GMT  
 Dynamically loading an assembly?
Put the interface definition in an assembly that it is referenced by various
plug-ins and main client application (so that you can cast to the interface
later)
Then use:
Imports PlugInInterfaces
Dim oAss As [Assembly] =[Assembly].LoadFrom("C:\your_path\pluginserver.dll")
Dim oType As Object = oAss.CreateInstance("PlugInServer.MainClass")
Dim oMainClass As MainInterface = CType(oType, MainInterface)
oMainClass.test()

Where MainInterface is your interface

HTH

--
Corrado Cavalli [Microsoft MVP]
UGIdotNET - http://www.ugidotnet.org



Sat, 28 May 2005 04:17:20 GMT  
 Dynamically loading an assembly?


Thanks.  But one question, what if the plugin is developed by a third
party?  I may not have any control over the class name.

Quote:
> Object = oAss.CreateInstance("PlugInServer.MainClass")

Can you suggest a way for determining the class name they've chosen?  Or
should I just say "To create a plugin for my app, you MUST name the class
..." ?

Thanks again.

Chris

--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail



Sat, 28 May 2005 04:38:40 GMT  
 Dynamically loading an assembly?
You can use Reflection to inspect plug-in classes and detect which one
implements your interface, but having all plug-ins using a common entry
point class wouldn't be a bad idea.

HTH

--
Corrado Cavalli [Microsoft MVP]
UGIdotNET - http://www.ugidotnet.org



Sat, 28 May 2005 05:21:44 GMT  
 Dynamically loading an assembly?


Quote:
> You can use Reflection to inspect plug-in classes and detect which one
> implements your interface, but having all plug-ins using a common entry
> point class wouldn't be a bad idea.

> HTH

Thanks again

--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail



Sat, 28 May 2005 05:29:37 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do I load and run assemblies dynamically

2. Load Assembly /CreateInstance

3. Load Assembly

4. One or more of the types in the assembly unable to load

5. Loading control from assembly

6. Trying to Dunamically Load Assembly via Reflection

7. Loading an assembly at runtime

8. Types in the assembly failed to load

9. Can not load file or assembly

10. could not load file or assembly

11. Could not load file or assembly

12. Assembly and Global Assembly Cache

 

 
Powered by phpBB® Forum Software