473,657 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Casting

Experience posters,

I am an experienced vb/vb.net developer but having a bit of trouble
converting a bit of code to C#. I have 3 projects in one solution.

Trying to create a plug-in type framework.

Project 1. Main winforms exe

a. Has a reference to project 2
b. Attempts to dynamically load all dlls found in a plug-in directory

Project 2. class library (plug-in interface)

a. Defines nothing but a simple interface.

Project 3. class library (plug-in)

a. References project 2
b. has a simple class that implements interface from project 2

My problem is; In the winforms app, I can't cast an object (dynamically
loaded from project 3) to the type of interface defined in project 2.
(invalid cast exception)

From googling for a couple of hours and reading many posts related to this
subject, I still can't figure out what I am doing incorrectly. I would
presume it is something simple. I also read that each type should be
uniquely defined by an essembly, but I thought I was doing that.

Thanks for any help.

Code:
From project 1.
// check each dll

foreach (string file in expansions)

{
Assembly dll = LoadAssembly(fi le);

// check each type

foreach (Type type in dll.GetTypes())

{

// only look at stuff that is public and not abstract

if (type.IsPublic == true && type.Attributes != TypeAttributes. Abstract &&
type.IsClass == true)

{

Type it = typeof(IArena);

if (type == typeof(IArena))

{

MessageBox.Show ("IArena Found");

}

try

{

IArena a = (IArena)type;

}

catch (Exception e)

{

MessageBox.Show (e.Message);

}
}

}
} // end for each string in expansions

From Project 2

using System;

using MTV3D65;

namespace ExpansionServic es

{

/// <summary>

/// Summary description for IMesh.

/// </summary>

public interface IArena

{

string Name

{

get;

set;

}
TVMesh Load();

}

}

From Project 3

using System;

using MTV3D65;

using ExpansionServic es;

namespace Rudius.Model_Cl asses

{

/// <summary>

/// Summary description for Arena1.

/// </summary>

public class Arena1 : IArena

{

private string name;
public string Name

{

get

{

return name;

}

set

{

name = value;

}

}
public Arena1()

{

this.Name = "Arena 1";

}

public TVMesh Load()

{
TVScene scene = new TVScene();

TVMesh mesh = scene.CreateMes hBuilder(this.n ame);

mesh.LoadTVM("m odels/meshes/arenas/arena.tvm", true, true);

mesh.SetLightin gMode(CONST_TV_ LIGHTINGMODE.TV _LIGHTING_MANAG ED);

mesh.ComputeNor mals();

mesh.SetScale(0 .1f, 0.1f, 0.1f);

return mesh;

}
}

}


Nov 17 '05 #1
8 3645
Try reading Jon Skeet's page on plug-ins:

http://www.yoda.arachsys.com/csharp/plugin.html

Nov 17 '05 #2
Chris Smith <us**@email.com > wrote:
I am an experienced vb/vb.net developer but having a bit of trouble
converting a bit of code to C#. I have 3 projects in one solution.

Trying to create a plug-in type framework.

Project 1. Main winforms exe

a. Has a reference to project 2
b. Attempts to dynamically load all dlls found in a plug-in directory

Project 2. class library (plug-in interface)

a. Defines nothing but a simple interface.

Project 3. class library (plug-in)

a. References project 2
b. has a simple class that implements interface from project 2


That sounds right to me. Could you provide a short but *complete*
program that demonstrates the problem? (If you could paste it in a way
that didn't make it have an empty line between each real line, that
would help too :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
> foreach (Type type in dll.GetTypes())
..
..
..
..
IArena a = (IArena)type;


you are trying to cast a Type object to IArena. that's an invalid cast.
what you should be doing is create an instance of that type and then cast the
instance to IArena.
Nov 17 '05 #4
Daniel Jin <Da*******@disc ussions.microso ft.com> wrote:
IArena a = (IArena)type;


you are trying to cast a Type object to IArena. that's an invalid cast.
what you should be doing is create an instance of that type and then cast the
instance to IArena.


Doh! Well spotted. Why do I always assume it'll be something subtle? ;)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Thanks,

Silly me,

type -> interface... that is just silly.

There is a method on the type object (.getinterface( "string")) I am
striving to not hard code the interface names. Is there a way to check the
type for an implementation of the interface, without knowing the interface
string name?
"Daniel Jin" <Da*******@disc ussions.microso ft.com> wrote in message
news:9C******** *************** ***********@mic rosoft.com...
foreach (Type type in dll.GetTypes())

.
.
.
.
IArena a = (IArena)type;


you are trying to cast a Type object to IArena. that's an invalid cast.
what you should be doing is create an instance of that type and then cast
the
instance to IArena.


Nov 17 '05 #6
Thanks,

Silly me,

type -> interface... that is just silly.

There is a method on the type object (.getinterface( "string")) I am
striving to not hard code the interface names. Is there a way to check the
type for an implementation of the interface, without knowing the interface
string name?
"Daniel Jin" <Da*******@disc ussions.microso ft.com> wrote in message
news:9C******** *************** ***********@mic rosoft.com...
foreach (Type type in dll.GetTypes())

.
.
.
.
IArena a = (IArena)type;


you are trying to cast a Type object to IArena. that's an invalid cast.
what you should be doing is create an instance of that type and then cast
the
instance to IArena.


Nov 17 '05 #7
Thanks,

Silly me,

type -> interface... that is just silly.

There is a method on the type object (.getinterface( "string")) I am
striving to not hard code the interface names. Is there a way to check the
type for an implementation of the interface, without knowing the interface
string name?

"Chris Smith" <us**@email.com > wrote in message
news:u9******** ********@TK2MSF TNGP10.phx.gbl. ..
Experience posters,

I am an experienced vb/vb.net developer but having a bit of trouble
converting a bit of code to C#. I have 3 projects in one solution.

Trying to create a plug-in type framework.

Project 1. Main winforms exe

a. Has a reference to project 2
b. Attempts to dynamically load all dlls found in a plug-in directory

Project 2. class library (plug-in interface)

a. Defines nothing but a simple interface.

Project 3. class library (plug-in)

a. References project 2
b. has a simple class that implements interface from project 2

My problem is; In the winforms app, I can't cast an object (dynamically
loaded from project 3) to the type of interface defined in project 2.
(invalid cast exception)

From googling for a couple of hours and reading many posts related to this
subject, I still can't figure out what I am doing incorrectly. I would
presume it is something simple. I also read that each type should be
uniquely defined by an essembly, but I thought I was doing that.

Thanks for any help.

Code:
From project 1.
// check each dll

foreach (string file in expansions)

{
Assembly dll = LoadAssembly(fi le);

// check each type

foreach (Type type in dll.GetTypes())

{

// only look at stuff that is public and not abstract

if (type.IsPublic == true && type.Attributes != TypeAttributes. Abstract &&
type.IsClass == true)

{

Type it = typeof(IArena);

if (type == typeof(IArena))

{

MessageBox.Show ("IArena Found");

}

try

{

IArena a = (IArena)type;

}

catch (Exception e)

{

MessageBox.Show (e.Message);

}
}

}
} // end for each string in expansions

From Project 2

using System;

using MTV3D65;

namespace ExpansionServic es

{

/// <summary>

/// Summary description for IMesh.

/// </summary>

public interface IArena

{

string Name

{

get;

set;

}
TVMesh Load();

}

}

From Project 3

using System;

using MTV3D65;

using ExpansionServic es;

namespace Rudius.Model_Cl asses

{

/// <summary>

/// Summary description for Arena1.

/// </summary>

public class Arena1 : IArena

{

private string name;
public string Name

{

get

{

return name;

}

set

{

name = value;

}

}
public Arena1()

{

this.Name = "Arena 1";

}

public TVMesh Load()

{
TVScene scene = new TVScene();

TVMesh mesh = scene.CreateMes hBuilder(this.n ame);

mesh.LoadTVM("m odels/meshes/arenas/arena.tvm", true, true);

mesh.SetLightin gMode(CONST_TV_ LIGHTINGMODE.TV _LIGHTING_MANAG ED);

mesh.ComputeNor mals();

mesh.SetScale(0 .1f, 0.1f, 0.1f);

return mesh;

}
}

}

Nov 17 '05 #8
Chris Smith <us**@email.com > wrote:
type -> interface... that is just silly.

There is a method on the type object (.getinterface( "string")) I am
striving to not hard code the interface names. Is there a way to check the
type for an implementation of the interface, without knowing the interface
string name?


If you don't know *anything* about it, you can't test for it. What *do*
you know?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

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

Similar topics

1
4072
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation : (simplified code, not a direct quote) $dob = mktime(0,0,0, 5, 3, 1983); if ((float)$dob > (float)strtotime("-18years")) {
5
8255
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new Derived(); Base* b1 = static_cast<Base*>(d); Base* b2 = (Base*)d; // traditional type casting Such is my understanding from code samples, my own uses, and this: http://www.cplusplus.com/doc/tutorial/tut5-4.html
1
9134
by: JohnK | last post by:
under the covers is type casting in VB.Net the same as C# ? myObject = CType(..,..) in VB.Net vs myObject = (SomeClass)aObject
1
3429
by: chook | last post by:
Wherein differences between type casting in C++ : static_cast, dinamic_cast, reinterpret_cast, const_cast and C type casting, like xxx = (type)yyy; What, when and why is necessary to use?
9
4032
by: Roman Mashak | last post by:
Hello, All! Given the sample piece of code I have: #include <stdio.h> #include <string.h> int main(void) { short int i, j;
7
3198
by: Wayne M J | last post by:
I have worked out most type casting and the likes but I am curious about one aspect. Endpoint ep...; IPEndPoint iep...; .... ep = (EndPoint)iep; .... iep = (IPEndPoint)ep; ....
23
3504
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement).
16
12785
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
7
4215
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an invisible cast is made? Is there any reason why one would use databinder.eval while in VB.NET? I can see why one might use it in C# so as to avoid specifying the type for the cast but since this is not necessary in VB.NET, I'm not sure I follow. ...
11
32384
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? class Derived (Base): def __init__ (self, base_object): # ( copy all attributes ) ...
0
8319
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
8837
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
8739
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...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.