473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Trying to create\use plugins causes casting issue.

I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin, not the entire app.

I have created a base interface, it looks like:

public interface myModel
sub Initialize(byref DisplayPanel)
sub Connect()
sub Disconnect()
sub Query()
...
end interface
I have created 3 projects. The container, the plugin and the model. I have put this interface file in all three projects. The third project does not do anything, but stores the model for me.
I have created a plugin, it lookes like:

public class myPlugin1
implements myModel

private dbConn as ???? - this is an object of perticular type.
private ResultCount as long
private isConnected as boolean

public sub Initialize(byref DisplayPanel as panel) implements myModel.Initialize
....
end sub

(and so forth)
end class

I build this as a DLL.

I then make my "container" application. I use the following code to load\test for my plugins:

Public Class PluginServices

Public Structure AvailablePlugin
Public AssemblyPath As String
Public ClassName As String
End Structure

Public Shared Function FindPlugins(ByVal strPath As String, ByVal strInterface As String) As AvailablePlugin()
Dim Plugins As ArrayList = New ArrayList
Dim strDLLs() As String, intIndex As Integer
Dim objDLL As [Assembly]

'Go through all DLLs in the directory, attempting to load them
strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll")
For intIndex = 0 To strDLLs.Length - 1
Try
objDLL = [Assembly].LoadFrom(strDLLs(intIndex))
ExamineAssembly(objDLL, strInterface, Plugins)
Catch e As Exception
'Error loading DLL, we don't need to do anything special
End Try
Next

'Return all plugins found
Dim Results(Plugins.Count - 1) As AvailablePlugin

If Plugins.Count <> 0 Then
Plugins.CopyTo(Results)
Return Results
Else
Return Nothing
End If
End Function

Private Shared Sub ExamineAssembly(ByVal objDLL As [Assembly], ByVal strInterface As String, ByVal Plugins As ArrayList)
Dim objType As Type
Dim objInterface As Type
Dim Plugin As AvailablePlugin

'Loop through each type in the DLL
For Each objType In objDLL.GetTypes
'Only look at public types
If objType.IsPublic = True Then
'Ignore abstract classes
If Not ((objType.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then

'See if this type implements our interface
objInterface = objType.GetInterface(strInterface, True)

If Not (objInterface Is Nothing) Then
'It does
Plugin = New AvailablePlugin
Plugin.AssemblyPath = objDLL.Location
Plugin.ClassName = objType.FullName
Plugins.Add(Plugin)
End If

End If
End If
Next
End Sub

Public Shared Function CreateInstance(ByVal Plugin As AvailablePlugin) As Object
Dim objDLL As [Assembly]
Dim objPlugin As Object

Try
'Load dll
objDLL = [Assembly].LoadFrom(Plugin.AssemblyPath)

'Create and return class instance
objPlugin = objDLL.CreateInstance(Plugin.ClassName)
Catch e As Exception
Return Nothing
End Try

Return objPlugin
End Function

End Class
I then try to connect to my plugin later in my "container" application. I do this with:
Private Sub lstSourceSystem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSourceSystem.SelectedIndexChanged
' Here we connect to the ImportController and show its connection properties
Try
Dim tmpControl As SourceModel
Dim dblResult As Double
**>> tmpControl = DirectCast(PluginServices.CreateInstance(Plugins(l stSourceSystem.SelectedIndex)), myModel)

tmpControl.Initialize(objHost)

frmSystemConnection.Show()

tmpControl.ShowInterface(Me.frmSystemConnection)

Catch ex As Exception
MsgBox(ex.Message)
frmSystemConnection.Hide()
ImportControl = Nothing
End Try
End Sub

I get a cast error on the line starting with **>>
I have tried just using CAST, and tried not using any type of casting. same results.
What am I missing. I have been searching the internet, and I have found examples of how I should do this, but I keep getting the same results.

--------------------------------
From: Greg Conely

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>OUzq1+c3Z0SigQG9wJ15AQ==</Id>
Nov 20 '05 #1
0 1565

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: stu_pb | last post by:
I am designing a plugin system for a window application using .NET(C# specifically). One of the requirements of the plugin system is to be able to dynamically load/unload plugins. My initial...
5
by: ThunderMusic | last post by:
Hi, I have some code to load some plug-ins, but the code requires me to know the name of the class to load (here: SamplePlugin, derived from IPlugin) : (Here is some C# code, but I use VB.Net to...
5
by: Christoph Haas | last post by:
Dear coders... I'm working on an application that is supposed to support "plugins". The idea is to use the plugins as packages like this: Plugins/ __init__.py Plugin1.py Plugin2.py...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
5
by: Mike D Sutton | last post by:
Hi, I'm porting an old project over to C# which makes heavy use of plugins, however I'm not sure the way I've approached it is the best technique. In the old project, I had a TypeLib with the...
3
by: Eric_Dexter | last post by:
I am having trouble trying to reuse the code that was provided in the wxdemo package of wxpython. The program I am trying to use parts of is Grid_MegaExample.py thier code is class...
1
by: krach.aran | last post by:
I'm creating a winforms click-once application that is plugin enabled. The application is working, and so are the plugins. but now i have got the following problem: after deployment i do not...
1
by: Arif Mohammed | last post by:
Hi, iam using MySql 4.0.1 and jboss-4.0.4.GA Iam getting the following exception when there are more concurrent requests more than 50 in a second Caused by: org.xyz.MyClass: SQL...
2
by: Michel Couche | last post by:
Hello, Normally automatic converters do a very good job at translating code between C# and VB I have a specific issue however that I can not solve using these converters .... and I just can...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.