473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Invalid CastException' 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 intCurrentModul eIndex As Integer

Private Sub frmMain_Load(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim PluginAssembly As System.Reflecti on.Assembly
Dim CommonAssembly As System.Reflecti on.Assembly
Dim t As Type
Dim i As Type
Dim x As Object 'IBWModule
Dim w As IBWModule
Dim typModule As Type

PluginAssembly =
System.Reflecti on.Assembly.Loa dFrom("C:\Docum ents and Settings\mjozwi k_2\My
Documents\Visua l Studio
Projects\BookWo rks\BookWorksIn ventory\bin\Boo kWorksInventory .dll")
CommonAssembly =
System.Reflecti on.Assembly.Loa dFrom("C:\Docum ents and Settings\mjozwi k_2\My
Documents\Visua l Studio
Projects\BookWo rks\BookWorksCo mmon\bin\BookWo rksCommon.dll")

typModule = CommonAssembly. GetType("BookWo rksCommon.IBWMo dule")
colModules = New Collection

For Each t In PluginAssembly. GetTypes
For Each i In t.GetInterfaces
If i.Equals(typMod ule) Then
10 x = Activator.Creat eInstance(t)
colModules.Add( x)
20 w = CType(x,IBWModu le)
End If
Next
Next
End Sub
End Class
End Namespace
Nov 15 '05 #1
4 1408
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.co m>
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.Reflecti on.Assembly.Loa dFrom("C:\Docum ents and Settings\mjozwi k_2\My
Documents\Visua l Studio
Projects\BookWo rks\BookWorksCo mmon\bin\BookWo rksCommon.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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.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.Reflecti on.Assembly.Loa dFrom("C:\Docum ents and Settings\mjozwi k_2\My Documents\Visua l Studio
Projects\BookWo rks\BookWorksCo mmon\bin\BookWo rksCommon.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.co m>
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
3791
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 (this has nothing to do with the Module-Class of the framework. it's selfmade). Now when I instantiate an object dynamically from an assembly created
3
3411
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 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
4
1871
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 provide certain functionality. In my form code, I'm not going to be sure of what types of controls the form will be using until run-time. So, I created an array of controls, all cast as Control. This all works fine, so far. Now, in the form code,...
7
3677
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; but it complains for the following lines
2
6379
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 with that interface. The casting is invalid but I can't see why, as the class I load from the assembly really is of the type I cast it to. Command a = (Command) t.GetConstructor(new...
0
1601
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, 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() ...
2
2168
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 has similar structure of the classes in my application. The test worked fine, the casting I want to do must work. I compared the structure of the test with the application to ensure they where similar (inheriting twice, base class implements...
6
1321
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 in this situation but I am having trouble getting access to properties at the presentation level. So my situation is I have a Vehicle base class. Public Class Vehicle Property BodyType as String End Class
1
240
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 a derived class of BaseProduct. BaseProduct product= new ChildProduct();
7
13705
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
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.