[Mono-list] IsSubclassOf and Assembly.LoadFrom problem

Elliott Draper el at eldiablo.co.uk
Wed Jan 4 20:47:17 EST 2006


I'm not entirely sure IsSubclassOf is meant to return true for classes 
implementing an interface? Certainly I've found that it hasn't in the 
past, and instead I use GetInterface() I think it is, passing in the 
name of the interface I'm looking for, and checking to see if that 
returns the interface type, or a null. This would change the code below 
from:

if (t.IsSubclassOf(typeof(a.a))) {

to:

if (t.GetInterface(typeof(a.a).FullName)!=null) {


Maybe someone else can comment on whether or not IsSubclassOf *should* 
be returning true for interfaces, but in the meantime I hope the above 
information should help you out.

Let me know how you get on,
-= El =-

Colin JN Breame wrote:

>I've come across this problem before but never solved it (even after searching 
>the internet for several hours!).  So I thought I'd ask here....
>
>I have two dlls:
>	a.dll - contains interface a.a
>	b.dll - contains class b.b that implements a.a
>
>A main program loads b.dll and tests each type in the assembly to find out if 
>it implements a.a.  This all works except the class b.b (that I know 
>implements a.a) says that it doesn't! e.g. IsSubclassOf returns false.
>
>I think that this might have something to do with AppDomains or some other 
>restriction of loading and using types from a dynamically loaded assembly.  
>I'm really stuck with this (got that banging my head against a brick wall 
>feeling...) so any help would be greatly appreciated.
>
>Below is a test setup if you're wondering what I mean...
>
>Thanks,
> -- Colin
>
>
>a.cs:
>namespace a {
>  public interface a {
>    string hello();
>  }
>}
>
>
>b.cs:
>namespace b {
>  public class b : a.a {
>    public string hello() {
>      return "hello world";
>    }
>  }
>}
>
>
>main.cs:
>using System;
>using System.Reflection;
>
>namespace test {
>  public class test {
>    public static void Main() {
>      Assembly a = Assembly.LoadFrom("b.dll");
>
>      Type[] types = a.GetTypes();
>      foreach (Type t in types) {
>        if (t.IsSubclassOf(typeof(a.a))) {
>          Console.WriteLine("{0} is a subclass of {1}", t, typeof(a.a));
>        } else {
>          Console.WriteLine("{0} is not a subclass of {1}", t, typeof(a.a));
>        }
>      }
>    }
>  }
>}
>
>
>build.sh:
>mcs -target:library -out:a.dll a.cs &&
>mcs -target:library -out:b.dll -r:a.dll b.cs &&
>mcs -out:test.exe -r:a.dll test.cs
>_______________________________________________
>Mono-list maillist  -  Mono-list at lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-list
>  
>



More information about the Mono-list mailing list