예제 #1
0
        /// <summary>
        /// Attempts to locate the named assembly within the current app domain or tries
        /// to load from path in filesystem.
        /// </summary>
        /// <returns>
        /// The assembly.
        /// </returns>
        /// <param name='name'>
        /// Assembly name as in "bridge.indicators"
        /// </param>
        public static Assembly FindAssembly(string name)
        {
            if (name == null)
            {
                throw new ArgumentException("tried to load an assembly with NULL name");
            }

            if (name.EndsWith(".dll") || name.EndsWith(".exe"))
            {
                name = StringUtils.RTrimField(name, 1, '.');
            }

            // if is a system assembly, load from fully qualitied name
            if (_system_assemblies.ContainsKey(name))
            {
                return(Assembly.Load(_system_assemblies[name]));
            }

            // first search loaded assemblies
            AppDomain domain   = AppDomain.CurrentDomain;
            Assembly  assembly = Collections.FindOne(
                domain.GetAssemblies(), (a) => string.Compare(name, a.GetName().Name, true) == 0);

            if (assembly != null)
            {
                return(assembly);
            }

            // now try to find in filesystem
            var path = StringUtils.Or(
                ResourceLoader.Find("lib/" + name + ".dll"),
                ResourceLoader.Find(name + ".dll"));

            // try to find the assembly without the full rooted name
            if (path == null && Path.IsPathRooted(name))
            {
                return(FindAssembly(Path.GetFileName(name)));
            }

            if (path == null)
            {
                throw new ArgumentException("could not find assembly: " + name);
            }

            var pdir = Directory.GetParent(path);
            var pcwd = Environment.CurrentDirectory;

            Environment.CurrentDirectory = pdir.FullName;

            try
            { assembly = Assembly.LoadFrom(path); }
            finally
            { Environment.CurrentDirectory = pcwd; }

            return(assembly);
        }