InvalidCastException in Dynamic Library

M

melwinphilip

Hi,

I'm new to C# and I'm wondering what I'm doing wrong here....

I've created a library (using csc/t:library) with the following
source...

*******************
using System;

interface ITest {
void M1();
}
class Class1:ITest{

public void M1() {
Console.WriteLine("In M1()");
}
}
*******************


Now I create a client with the following source

*******************
using System;
using System.Reflection;

interface ITest {
void M1();
}

class DynamicInvoke
{
public static void Main(String [] args)
{
String path = "Class1";
Assembly a = Assembly.Load(path);

Type mm = a.GetType("Class1");
ITest it = (ITest)Activator.CreateInstance(mm); <-- Exception
it.M1();
}
}

*******************

I get a the following exception
"Unhandled Exception: System.InvalidCastException: Specified cast is
not valid.
at DynamicInvoke.Main(String[] args)"

Plz help...

Regds,
Melwin.
 
A

Andrew Kirillov

Hello



It's obvious that you got the cast exception.



You declared ITest interface in your library and in your client application.
So, you got two types. Although, these declarations are identical, these
types are different for CLR. One type's full name is something like this -
YourLib.ITest, but the second type's name is YourApp.ITest. Your class is
implementing the first interface, not the one, so the cast is incorrect.



You should remove ITest declaration from client application, and just add a
reference to your library and put "using MyLib". But, in these case, it will
be easer just to instantiate your Class1 directly.
 
M

melwinphilip

Thanks Andrew.

I still have the following doubt though....

If I create my interface alone as a new file and compile it as a dll
and reference it to the the client as well as the original library,
everything works ok.

But, if I use the csc/t:module option and make a module out of the new
interface file, and then use the csc/addmodule option while compiling
the library and the client, the old error occurs while running.

Whats the difference between these two ways?
Regds,
 
J

Jon Skeet

But, if I use the csc/t:module option and make a module out of the new
interface file, and then use the csc/addmodule option while compiling
the library and the client, the old error occurs while running.

If you use modules, you're still creating the type in two different
assemblies, so there are two different types.

When both the client and the original library refer to the same third
assembly, only one type is involved.

Jon
 
A

Andrew Kirillov

If I create my interface alone as a new file and compile it as a dll
and reference it to the the client as well as the original library,
everything works ok.

Let's clarify things. If you will place your interface in a separate
library, and both your application and your another library will reference
that library with your interface definition, then all will work fine.
But, if I use the csc/t:module option and make a module out of the new
interface file, and then use the csc/addmodule option while compiling
the library and the client, the old error occurs while running.

Do you still have two declarations of your interface in the case ?
 
D

dvroegop

The reason you're getting the error is that creating the new assembly
(even though you are still refering to the same module) you are
basically creating a new type. Types are bound to the assembly. The
best way to do this is indeed to create an assembly containing the
interface and use that in both your projects. That way there is only on
type with the name ITest, so the framework will know what you are
talking about when you are referencing ITest.
 

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