How bout something like this?

var mainAssembly = Assembly.GetExecutingAssembly();
var referencedAssemblies = mainAssembly.GetReferencedAssemblies();
List<Assembly> allAssemblies = new List<Assembly>();
allAssemblies.Add(mainAssembly);
foreach (AssemblyName an in referencedAssemblies) {
allAssemblies.Add(Assembly.Load(an));
}
var assemblies = allAssemblies.ToArray();

On Wed, Feb 23, 2011 at 1:09 PM, Paul Jones <jonesy_bo...@hotmail.com>wrote:

>  Yes AFAIK GetAssemblies only contains the assemblies that have been loaded
> so far (will only happen when it is needed). There is
> GetReferencedAssemblies -
> http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies.aspx-
>  though that is not the same.
>
> ------------------------------
> Date: Wed, 23 Feb 2011 09:49:33 +0800
>
> Subject: Re: Getting all instances of a type from all assemblies
> From: michael.minuti...@gmail.com
> To: ozdotnet@ozdotnet.com
>
>
> This code will work in Autofac
>
>             var builder = new ContainerBuilder();
>
>  builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
>                 .As<IStartup>();
>
>             var container = builder.Build();
>             var startups = container.Resolve<IEnumerable<IStartup>>();
>
> I found that I needed to access at least one class from an assembly for the
> assembly to be loaded into the AppDomain in my quick tests. Perhaps someone
> has an explanation or a workaround for that.
>
>
>
> On Wed, Feb 23, 2011 at 9:29 AM, Mark Hurd <markeh...@gmail.com> wrote:
>
> Yeah, and there's probably an issue with the covariant array interface
> implementations too.
>
> On 23 February 2011 11:37, Bill McCarthy
> <bill.mccarthy.li...@live.com.au> wrote:
> > Yep. I seem to recall there being a bit of a hiccup with GetInterfaces in
> > that you might have to do a recursive on the base types. Can't recall if
> > that was only with generic interfaces or not though
> >
> > |-----Original Message-----
> > |From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-
> > |boun...@ozdotnet.com] On Behalf Of Mark Hurd
> > |Sent: Wednesday, 23 February 2011 11:59 AM
> > |To: ozDotNet
> > |Subject: Re: Getting all instances of a type from all assemblies
> > |
> > |Using reflection, if your assemblies have been loaded, it is simply
> > enumerating
> > |AppDomain.GetCurrentDomain.GetAssemblies, then for each assembly
> > |GetTypes, and finally for each type GetInterfaces. (That is off the top
> of
> > my
> > |head without looking up the details.)
> > |
> > |--
> > |Regards,
> > |Mark Hurd, B.Sc.(Ma.)(Hons.)
> > |
> > |On 23 February 2011 11:21, Paul Jones <jonesy_bo...@hotmail.com> wrote:
> > |> G'Day programmers,
> > |> I've been away from the coding game for a few years now so take it
> > |> easy on me please.
> > |> I'm trying to get all implementations of an interface (IStartup) in
> > |> all of my assemblies (main executable and referenced class libraries)
> > |> but having no luck doing this dynamically (with no hard coding of type
> or
> > |assembly names).
> > |> I've attempted to do it manually using classes in System.Reflection
> > |> but happy to use an IoC container which I imagine is possible for this
> > |> type of requirement.
> > |> Any help or hints would be appreciated.
> > |> Cheers,
> > |> Paul
>
>
>

Reply via email to