473,387 Members | 1,541 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,387 software developers and data experts.

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(file);

// 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 ExpansionServices

{

/// <summary>

/// Summary description for IMesh.

/// </summary>

public interface IArena

{

string Name

{

get;

set;

}
TVMesh Load();

}

}

From Project 3

using System;

using MTV3D65;

using ExpansionServices;

namespace Rudius.Model_Classes

{

/// <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.CreateMeshBuilder(this.name);

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

mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGH TING_MANAGED);

mesh.ComputeNormals();

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

return mesh;

}
}

}


Nov 17 '05 #1
8 3615
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.com>
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*******@discussions.microsoft.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.com>
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*******@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.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*******@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.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****************@TK2MSFTNGP10.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(file);

// 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 ExpansionServices

{

/// <summary>

/// Summary description for IMesh.

/// </summary>

public interface IArena

{

string Name

{

get;

set;

}
TVMesh Load();

}

}

From Project 3

using System;

using MTV3D65;

using ExpansionServices;

namespace Rudius.Model_Classes

{

/// <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.CreateMeshBuilder(this.name);

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

mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGH TING_MANAGED);

mesh.ComputeNormals();

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.com>
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
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 :...
5
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...
1
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
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
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
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
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...
16
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
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...
11
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? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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,...

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.