473,408 Members | 2,477 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,408 software developers and data experts.

Casting an object as an Interface that it implements

First off, this is a VB application, but I know you C# are used to working
at the lower levels like this, so I'm hoping this is an easy one for you.
Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat
proficient in C#, but my bosses and coworkers barely know VB, so no, I can't
just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this
one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.

I am basically attempting to create a "plug-in" feature for my app.

Namespace A contains the plug-in interface
Namespace B is a sample implementation of that interface.
The application needs to be able to load the implementation in Namespace B
and use it via the interface.

Line 10 is where I create an instance of class BWInventory
Line 20 is where I get the following error when I try to get a reference to
the Interface

"An unhandled exception of type 'System.InvalidCastException' occurred in
BookWorks.exe
Additional Information: Specified cast is not valid"

I can continue to use it as an "object" and I can even call the interface
methods, but for various reasons, I need to have a real reference to the
interface.
Help! Thanks!
Jeff

Namespace A ' DLL. referenced by App and B
Public Interface IBWModule
ReadOnly Property Name() As String
End Interface
End Namespace

Namespace B 'Loaded dynamically by App
Public Class BWInventory
Implements IBWModule

Public ReadOnly Property Name() As String Implements
BookWorksCommon.IBWModule.Name
Get
Return "BookWorks Inventory"
End Get
End Property
End Class
End Namespace

Namespace App 'References A and dynamically loads B.
Imports BookWorksCommon
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim WithEvents mfrmLogin As frmLogin
Dim colModules As Collection
Dim intCurrentModuleIndex As Integer

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim PluginAssembly As System.Reflection.Assembly
Dim CommonAssembly As System.Reflection.Assembly
Dim t As Type
Dim i As Type
Dim x As Object 'IBWModule
Dim w As IBWModule
Dim typModule As Type

PluginAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksInventory\bin\BookWork sInventory.dll")
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCo mmon.dll")

typModule = CommonAssembly.GetType("BookWorksCommon.IBWModule" )
colModules = New Collection

For Each t In PluginAssembly.GetTypes
For Each i In t.GetInterfaces
If i.Equals(typModule) Then
10 x = Activator.CreateInstance(t)
colModules.Add(x)
20 w = CType(x,IBWModule)
End If
Next
Next
End Sub
End Class
End Namespace
Nov 15 '05 #1
4 1391
Jeff Molby <JeffMolby@C_mc_st.n_t> wrote:
First off, this is a VB application, but I know you C# are used to working
at the lower levels like this, so I'm hoping this is an easy one for you.
Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat
proficient in C#, but my bosses and coworkers barely know VB, so no, I can't
just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this
one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.


I suspect the problem you've got is the one I've described here:
http://www.pobox.com/~skeet/csharp/plugin.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jeff Molby <JeffMolby@C_mc_st.n_t> wrote:
First off, this is a VB application, but I know you C# are used to working
at the lower levels like this, so I'm hoping this is an easy one for you.
Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat
proficient in C#, but my bosses and coworkers barely know VB, so no, I can't
just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this
one. I am now at the mercy of your good nature. <g>
<snip>
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCo mmon.dll")


I don't think you should load the common assembly by reflection - just
use the VB.NET equivalent of the C# "typeof" operator to get the common
type and cast to that.

I *think* that's probably the problem, but I wouldn't like to say for
sure.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
I just found it. Somewhat of a variation on what you described in your site.

Because the IDE doesn't copy the DLL to my main app's bin directory (there's
no static reference), I was just loading the DLL from its own bin directory.
However, That directory had its own copy of the Interface assembly, so it
behaved just as you described in your site.

Sorry about the stupid mistake and thank you for your help!

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Jeff Molby <JeffMolby@C_mc_st.n_t> wrote:
First off, this is a VB application, but I know you C# are used to working at the lower levels like this, so I'm hoping this is an easy one for you. Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat proficient in C#, but my bosses and coworkers barely know VB, so no, I can't just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.


I suspect the problem you've got is the one I've described here:
http://www.pobox.com/~skeet/csharp/plugin.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
I tried that, but VB's equivalent TypeOf statement can only be used in an If
statement and it can only compare against a hardcoded type, rather than type
object. It sucks...
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Jeff Molby <JeffMolby@C_mc_st.n_t> wrote:
First off, this is a VB application, but I know you C# are used to working at the lower levels like this, so I'm hoping this is an easy one for you. Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat proficient in C#, but my bosses and coworkers barely know VB, so no, I can't just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g>


<snip>
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCo mmon.dll")


I don't think you should load the common assembly by reflection - just
use the VB.NET equivalent of the C# "typeof" operator to get the common
type and cast to that.

I *think* that's probably the problem, but I wouldn't like to say for
sure.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5

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

Similar topics

2
by: Simon X-Session | last post by:
Hi ! I hope somebody can help me, i'm completly out of mind. My problem: I have a class inherited from Windows.Forms.Control named BasicModule, which implements an interface named IModule...
3
by: Jeff Molby | last post by:
Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g> I have 3 projects in my solution: One app and 2 libraries. I am...
4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
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;...
2
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work...
0
by: Greg Conely via .NET 247 | last post by:
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,...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
6
by: GarrettD78 | last post by:
Accidently posted this to the wrong group so I am reposting. This is probably a newbie question but I am a little confused about how to go next with my code. I think I want to use a factory pattern...
1
by: pitdog | last post by:
This may not be the group to post this in. However I will try... I am interested in how the .NET runtime actually handles casting. For instance... This will work if you assume ChildProduct is...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.