|
-
Jul 9th, 2009, 01:02 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Dynamic Object Creation from dll
I am trying to get the hang of Dynamic loading of objects from assemblies on the same machine (But NOT part of the same project).
In the sample below, Form1 implements an interface built as part of the .dll I placed in C:\Release.
I am trying to create a local instance of Form1, and Cast it as Iform, so that the local code can access the properties and methods exposed by the interface.
The assumption here is that at runtime, local code will have access to the String name of a form, and an interface which is implemented by the form (and is compiled into the .dll as well as the client application), and that is it.
Does this make sense?
When I run this code, I get a exception:
"Unable to cast object of type 'WindowsApplication1.Form1' to type 'WindowsApplication1.Iform'.
Code:
Public Function fApplicationObject() As Iform
Dim sLocation As String = "C:\Release\WindowsApplication1.dll"
Dim sType As String = "WindowsApplication1.Form1"
Dim dAssembly As [Assembly] = [Assembly].LoadFrom(sLocation)
Dim dType As Type = dAssembly.GetType(sType)
Dim dObj As Iform = Activator.CreateInstance(dType)
'The Exception happens HERE:
Dim frm As Iform = dObj
'I also tried several other ways;
'CType, Etc, and I even set a reference to the dll, and tried THIS: Dim frm As WindowsApplication1.Form1 = obj
Return frm
End Function
I seem unable to dynamically load an object from a dll and then utilize an interface to access it's properties and such.
What am I missing?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|