[Mono-list] Assembly.Load doubts

Mario Sopena mario.sopena at gmail.com
Thu Jul 28 15:06:11 EDT 2005

I have the following scenario:

------  TestBase.cs -----------
public interface TestBase {
	void Print();

------    Test.cs    -------------------
public class Test : TestBase 
	public Test () 	{}
	public void Print() 
		System.Console.WriteLine ("Hola");

------- TestClass.cs --------------------
using System;
using System.Reflection;
public class TestClass 
	public static void Main (string[] args) 
		new TestClass();

	public TestClass () 
		AssemblyName asna = AssemblyName.GetAssemblyName ("Test.dll");
		Assembly asm = Assembly.LoadWithPartialName (asna.FullName);
		Type t = asm.GetType("Test", true);
		object obj = Activator.CreateInstance (t);
		TestBase tb = (TestBase) obj;  //InvalidCastException

I compile like this: 
mcs -target:library Test.cs TestBase.cs
mcs -debug TestClass.cs TestBase.cs

So I end up with a Test.dll and TestClass.exe. Now, executing the
TestClass.exe throws an InvalidCastException in the line with the

I think the problem is that i don't load the dll in the correct
context, but I've tried other possibilities and I couldn't make it
work. Two days ago I didn't know was an AppDomain was, so there is the
possibility that I'm doing something really stupid.

Any help pleaase?

More information about the Mono-list mailing list