[Mono-list] Assembly.Load doubts

Rodolfo Campero rodolfo.campero at gmail.com
Thu Jul 28 15:14:06 EDT 2005


Hi,

On 7/28/05, Mario Sopena <mario.sopena at gmail.com> wrote:
> 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
>                 tb.Print();
>         }
> }
> 
> 
> 
> 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
> comment.
> 
> 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?

We just discussed this problem in monohispano:
https://listas.hispalinux.es/pipermail/mono-hispano/2005-July/003327.html
(I'm assuming that you speak spanish)

Cheers,
Rodolfo


More information about the Mono-list mailing list