So, here is what I have done...(This is going to be a long answer)
I created one solution in VS just to make the dlls. In that solution, I started with an
Interface
. It looks like:
Public Interface CameraInterface
ReadOnly Property Brand As String
ReadOnly Property Version As String
ReadOnly Property Model As String
Function FullInformation() As String
End Interface
Then, I created 3 other projects within that solution...one for a different type of camera. In those projects, I added the reference to the
InterfaceClass
(the
namespace
for
CameraInterface
). Then, in the class, I implemented the interface. It looks like:
Public Class CanonPowershot
Implements InterfaceClass.CameraInterface
Private Const _brand As String = "Canon"
Public ReadOnly Property Brand As String _
Implements InterfaceClass.CameraInterface.Brand
Get
Return _brand
End Get
End Property
Public Function FullInformation() As String _
Implements InterfaceClass.CameraInterface.FullInformation
Return _brand & " " & _model & ", v." & _version
End Function
Private Const _model As String = "Powershot"
Public ReadOnly Property Model As String _
Implements InterfaceClass.CameraInterface.Model
Get
Return _model
End Get
End Property
Private Const _version As String = "1.23"
Public ReadOnly Property Version As String _
Implements InterfaceClass.CameraInterface.Version
Get
Return _version
End Get
End Property
End Class
Once I had created these classes, I build the dll's.
From there, I created a new solution for testing the interface. Once again, I added a reference to the
InterfaceClass
. I created a simple form with a menu. The first menu item was my
LoadFiles
ToolStripMenuItem
. I created a simple form to allow the user to select the files to load and then copied them to a directory that I set up.
The other menu item simply lists the different dll's that have been loaded. To load the dll's, I first look for the directory, then get the files in the directory and look for the dlls. I have an array of assemblies that I use to store each camera type. As I add a new dll, I increment the array and create a new array from the file. Then, I have to see if there is a type within that array that I can use the interface for. To do that, I cycle through the types in the assembly and check if it has the interface that we need. If it does, I create a new instance of the type and create a new menu item. The LoadDlls method looks like:
Private Sub LoadDlls()
If Not Directory.Exists("D:\Temp\Dlls") Then
CamerasToolStripMenuItem.Enabled = False
Else
Dim enableCamera As Boolean = False
ReDim myAssembly(-1)
For Each st As String In Directory.GetFiles("D:\Temp\Dlls")
If Path.GetExtension(st) = ".dll" Then
enableCamera = True
ReDim Preserve myAssembly(UBound(myAssembly) + 1)
myAssembly(UBound(myAssembly)) = Assembly.LoadFrom(st)
Dim testInterface As CameraInterface
Dim currentAssembly As Integer = UBound(myAssembly)
With myAssembly(currentAssembly)
For i = 0 To .GetTypes.Count - 1
Try
If (.GetTypes()(i).GetInterface("CameraInterface")) IsNot Nothing Then
testInterface = TryCast(.CreateInstance(.GetTypes()(i).FullName), CameraInterface)
End If
Catch ex As Exception
testInterface = Nothing
End Try
If testInterface IsNot Nothing Then
Dim newItem As New ToolStripMenuItem(testInterface.Brand & " " & testInterface.Model)
CamerasToolStripMenuItem.DropDownItems.Add(newItem)
Exit For
End If
Next
End With
End If
Next
CamerasToolStripMenuItem.Enabled = enableCamera
End If
End Sub
You would have to keep track of which type came from which assembly and which type within that assembly, but you could create a simple struct with that information and add that struct to the menu item's tag property. Then, you could easily get to the right assembly and the right type any time you needed to use it.
As far as nested classes, I'm not sure about that or exactly what you're doing. I believe that you can have as many nested classes within an interface as you want. But you wouldn't have access to them using the interface. But, if you needed them to implement the interface methods, you could do that.