/// <summary>
        /// Uninstalls the assembly resolver.
        /// </summary>
        public static void Clear()
        {
            ChoAssemblyManager.Clear();

            AppDomain.CurrentDomain.AssemblyResolve -=
                new ResolveEventHandler(AssemblyResolve);

            AppDomain.CurrentDomain.AssemblyLoad -=
                new AssemblyLoadEventHandler(AssemblyLoad);
        }
 /// <summary>
 /// Occurs when an assembly is loaded. The loaded assembly is added
 /// to the assembly cache.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="args">An <see cref="AssemblyLoadEventArgs" /> that contains the event data.</param>
 private static void AssemblyLoad(object sender, AssemblyLoadEventArgs args)
 {
     ChoAssemblyManager.AddAssemblyToCache(args.LoadedAssembly);
 }
        /// <summary>
        /// Resolves an assembly not found by the system using the assembly
        /// cache.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">A <see cref="ResolveEventArgs" /> that contains the event data.</param>
        /// <returns>
        /// The loaded assembly, or <see langword="null" /> if not found.
        /// </returns>
        private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
        {
            bool isFullName = args.Name.IndexOf("Version=") != -1;

            // first try to find an already loaded assembly
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                if (isFullName)
                {
                    if (assembly.FullName == args.Name)
                    {
                        // return assembly from AppDomain
                        return(assembly);
                    }
                }
                else if (assembly.GetName(false).Name == args.Name)
                {
                    // return assembly from AppDomain
                    return(assembly);
                }
            }

            if (ChoAssemblyManager.ContainsAsMissingAssembly(args.Name))
            {
                return(null);
            }

            // find assembly in cache
            if (ChoAssemblyManager.ContainsAssembly(args.Name))
            {
                // return assembly from cache
                return((Assembly)ChoAssemblyManager.GetAssemblyFromCache(args.Name));
            }
            else
            {
                //String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll";

                //if (Assembly.GetExecutingAssembly().GetManifestResourceInfo(resourceName) != null)
                //{
                //    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                //    {
                //        Byte[] assemblyData = new Byte[stream.Length];

                //        stream.Read(assemblyData, 0, assemblyData.Length);

                //        return Assembly.Load(assemblyData);
                //    }
                //}

                string   assmeblyFileName = null;
                string[] asms             = args.Name.Split(new char[] { ',' });
                bool     fileFound        = false;
                foreach (string path in _paths)
                {
                    if (path == null || path.Trim().Length == 0)
                    {
                        continue;
                    }

                    assmeblyFileName = Path.Combine(path, asms[0] + ".dll");

                    if (File.Exists(assmeblyFileName))
                    {
                        fileFound = true;
                        break;
                    }
                }

                if (fileFound)
                {
                    //if (ChoCodeBase.Me.LoadAssemblyFromFile)
                    return(Assembly.LoadFile(assmeblyFileName));
                    //else
                    //    return Assembly.Load(ChoFile.ReadAllBytes(assmeblyFileName));
                }
                else if (!assmeblyFileName.IsNullOrEmpty())
                {
                    ChoAssemblyManager.AddMissingAssembly(args.Name);
                }
            }

            return(null);
        }