[Mono-list] IsSubclassOf and Assembly.LoadFrom problem

2006-01-04 Thread Colin JN Breame
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@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] IsSubclassOf and Assembly.LoadFrom problem

2006-01-04 Thread Elliott Draper
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@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list
 



___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] IsSubclassOf and Assembly.LoadFrom problem

2006-01-06 Thread Colin JN Breame
On Thursday 05 January 2006 01:47, Elliott Draper wrote:
>
> if (t.GetInterface(typeof(a.a).FullName)!=null) {
>

This seems to work, and you were right - IsSubclassOf does not return true for 
interfaces.  Thanks.

>
> 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@lists.ximian.com
> >http://lists.ximian.com/mailman/listinfo/mono-list

-- 
Colin JN Breame
Freelance IT Consulting
e: [EMAIL PROTECTED]
t: +44(0)207 2630931
m: 0793 147 0091
w: http://breame.net
j: [EMAIL PROTECTED]
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list