[Mono-list] Instantiating all classes from a namespace

Charlie Poole charlie at nunit.com
Sun Oct 5 13:58:08 EDT 2008


That doesn't instantiate them, which is much harder in general.

Say a Type has only one consructor, which takes an object of
class A. Now you have to instantiate A. But maybe A ony has
a constructor requiring a B... :-)

Hopefully, the OP doesn't want to solve the most general case, 
but has some specific limits he has not told us about yet.


Charlie

> -----Original Message-----
> From: mono-list-bounces at lists.ximian.com 
> [mailto:mono-list-bounces at lists.ximian.com] On Behalf Of Chris Howie
> Sent: Sunday, October 05, 2008 6:50 AM
> To: Esdras Beleza
> Cc: Mono mailing list
> Subject: Re: [Mono-list] Instantiating all classes from a namespace
> 
> On Mon, Sep 29, 2008 at 4:46 PM, Esdras Beleza 
> <linux at esdrasbeleza.com> wrote:
> > The question is: how can I iterate inside a namespace 
> classlist, and 
> > create a new instance of each class of it?
> 
> There is no cut and dried way to do this, but you can do 
> something like this:
> 
> IEnumerable<Type> GetTypesInNamespace(string namespace) {
>     foreach (Assembly assembly in 
> AppDomain.CurrentDomain.GetAssemblies()) {
>         foreach (Type type in assembly.GetTypes()) {
>             if (type.Namespace == namespace)
>                 yield return type;
>         }
>     }
> }
> 
> Or you can do something LINQy.  :)
> 
> --
> Chris Howie
> http://www.chrishowie.com
> http://en.wikipedia.org/wiki/User:Crazycomputers
> _______________________________________________
> 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