Type Casting

C

Chris Smith

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_LIGHTING_MANAGED);

mesh.ComputeNormals();

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

return mesh;

}


}

}
 
J

Jon Skeet [C# MVP]

Chris Smith said:
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 :)
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

Daniel Jin said:
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? ;)
 
C

Chris Smith

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?
 
C

Chris Smith

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?
 
C

Chris Smith

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?
 
J

Jon Skeet [C# MVP]

Chris Smith said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top