I finally found an interesting article after searching all day that helped
with the solution.
You can find it at:

http://www.ayende.com/Blog/archive/2006/05/22/7579.aspx

It works good.  I modified it so that you can use it with multiple AppDomain
objects.

Thanks,
Mike

Here's the code that I modified:

    /// <summary>
    /// Provides assembly resolution for a particular application domain.
    /// <remarks>
    /// The basis for this class was found at:
http://www.ayende.com/Blog/archive/2006/05/22/7579.aspx.
    /// The original code has been modified to allow multiple AppDomains to
provide assembly resolution as well.
    /// </remarks>
    /// </summary>
    public static class AssemblyLocator {

        private static Dictionary<AppDomain, Dictionary<string,
System.Reflection.Assembly>> Assemblies = new Dictionary<AppDomain,
Dictionary<string, System.Reflection.Assembly>>();

        /// <summary>
        /// Initializes the locator with the current AppDomain.
        /// </summary>
        public static void Init() {
            Init(AppDomain.CurrentDomain);
        }

        /// <summary>
        /// Initializes the locator with the specified domain.
        /// </summary>
        /// <param name="domain">The domain to monitor.</param>
        public static void Init(AppDomain domain) {
            if (!Assemblies.ContainsKey(domain)) {
                Assemblies.Add(domain, new Dictionary<string,
System.Reflection.Assembly>());
                domain.AssemblyLoad += new
AssemblyLoadEventHandler(Domain_AssemblyLoad);
                domain.AssemblyResolve += new
ResolveEventHandler(Domain_AssemblyResolve);
            }
        }

        private static System.Reflection.Assembly
Domain_AssemblyResolve(object sender, ResolveEventArgs args) {
            System.Reflection.Assembly assembly = null;
            Assemblies[sender as AppDomain].TryGetValue(args.Name, out
assembly);
            return assembly;
        }

        private static void Domain_AssemblyLoad(object sender,
AssemblyLoadEventArgs args) {
            Assemblies[sender as AppDomain][args.LoadedAssembly.FullName] =
args.LoadedAssembly;
        }
    }




On Wed, Jun 25, 2008 at 3:13 PM, Geoff Taylor <[EMAIL PROTECTED]>
wrote:

> I haven't done this in a long time (I think it was .NET 1.0, or maybe the
> beta), but at the time I had to add a handler to
> AppDomain.CurrentDomain.AssemblyResolve to help with type resolution.
>
> Trying that might help get you up and running, but it's probably not the
> 'right' answer.
>
> Good luck,
>
> Geoff
>
> On Wed, Jun 25, 2008 at 6:32 PM, Mike Andrews <[EMAIL PROTECTED]
> >
> wrote:
>
> > I need some help with dynamic assembly loading.
> >
> > I need to load a single assembly and its dependent assemblies (and theirs
> > too if required).  I already have a method that will get the assembly and
> > dependent assemblies.
> >
> > I have this  section of code (this is test only):
> >
> >    foreach (var item in allAssemblies) {
> >        var assm = item.GetAssembly();
> >    }
> >
> >    foreach (var item in allAssemblies) {
> >        try {
> >            System.Reflection.Assembly ab = item.GetAssembly();
> >            Type[] types = item.GetAssembly().GetTypes();
> >        } catch (System.Reflection.ReflectionTypeLoadException ex) {
> >
> >        }
> >    }
> >
> > The GetAssembly() method above essentially does this:
> >
> > return System.Reflection.Assembly.Load(data, null,
> > this.GetType().Assembly.Evidence);
> >
> > The first assembly I come to is the primary assembly.  It has the type I
> > want to use.  The subsequent assemblies are the assemblies upon which the
> > primary assembly depends.
> > However, even though I've loaded all the assemblies, I get the
> > ReflectionTypeLoadException when I try to query the Types in the primary
> > assembly by using the GetTypes() method.
> >
> > Any suggestions on how I can accomplish this?
> >
> > Thanks,
> > Mike
> >
> > ===================================
> > This list is hosted by DevelopMentor(R)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to