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;
}
}
}
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;
}
}
}