470,612 Members | 2,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

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 3399
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by maxim vexler | last post: by
5 posts views Thread by Suzanne Vogel | last post: by
1 post views Thread by JohnK | last post: by
1 post views Thread by chook | last post: by
9 posts views Thread by Roman Mashak | last post: by
7 posts views Thread by Wayne M J | last post: by
23 posts views Thread by Renť Nordby | last post: by
16 posts views Thread by Enekajmer | last post: by
7 posts views Thread by Ben R. | last post: by
11 posts views Thread by Frederic Rentsch | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.