コード例 #1
0
            public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
            {
                var subset = searchedAssemblies[name.Name];

                var found = subset.FirstOrDefault(asm => asm.Version == name.Version) ?? subset.FirstOrDefault(asm => OpenTap.Utils.Compatible(asm.Version, name.Version));

                ReaderParameters customParameters = new ReaderParameters()
                {
                    AssemblyResolver = new TapMonoResolver()
                };

                if (found == null) // Try find dependency from already loaded assemblies
                {
                    var neededAssembly = new AssemblyName(name.ToString());
                    var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(s => s.GetName().Name == neededAssembly.Name);
                    if (loadedAssembly != null)
                    {
                        return(AssemblyDefinition.ReadAssembly(loadedAssembly.Location, customParameters));
                    }
                }

                if (found != null)
                {
                    return(AssemblyDefinition.ReadAssembly(found.Location, customParameters));
                }
                else
                {
                    return(base.Resolve(name, parameters));
                }
            }
コード例 #2
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            var assembly = SearchDirectory(name, m_directories);

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

#if !SILVERLIGHT && !CF
            var framework_dir = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, new[] { framework_dir });
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            if (name.Name == "mscorlib")
            {
                assembly = GetCorlib(name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

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

            assembly = SearchDirectory(name, new[] { framework_dir });
            if (assembly != null)
            {
                return(assembly);
            }
#endif

            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new FileNotFoundException(name.ToString());
        }
コード例 #3
0
        IEnumerable <Module> findModules(AssemblyNameReference assemblyRef)
        {
            var moduleHash = assemblyHash.lookup(assemblyRef.ToString());

            if (moduleHash != null)
            {
                return(moduleHash.Modules);
            }
            return(null);
        }
コード例 #4
0
 private bool IsExcludedDependency(ICustomAttributeProvider provider, AssemblyNameReference refer)
 {
     foreach (CustomAttribute attr in provider.CustomAttributes)
     {
         if (attr.AttributeType.FullName == "ExcludeDependencyAttribute" &&
             attr.ConstructorArguments[0].Value.ToString() == refer.ToString())
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #5
0
        public AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            var key = name.ToString();

            if (!params_cache.TryGetValue(key, out ReaderParameters parameters))
            {
                parameters = new ReaderParameters {
                    AssemblyResolver = this
                };
                params_cache [key] = parameters;
            }
            return(Resolve(name, parameters));
        }
コード例 #6
0
        public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            lock (resolutionCache)
            {
                string key = name.FullName;

                AssemblyDefinition result;
                if (resolutionCache.TryGetValue(key, out result))
                {
                    return(result);
                }
            }

            if (!name.Name.StartsWith("System", StringComparison.OrdinalIgnoreCase) &&
                !name.Name.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var asm    = Assembly.Load(name.ToString());
            var asmDef = AssemblyDefinition.ReadAssembly(
                asm.Location,
                parameters ??
                new ReaderParameters(ReadingMode.Deferred)
            {
                AssemblyResolver = this
            });

            lock (resolutionCache)
            {
                string key = name.FullName;

                AssemblyDefinition result;
                if (resolutionCache.TryGetValue(key, out result))
                {
                    return(result);
                }

                resolutionCache[key] = asmDef;
            }

            return(asmDef);
        }
コード例 #7
0
        public static Reflector.CodeModel.IAssemblyReference Cecil2Reflector(AssemblyNameReference anr, ModuleDefinition md)
        {
            Reflector.CodeModel.IModule m = Cecil2Reflector(md);
            if (m == null)
            {
                return(null);
            }

            string anrName = anr.ToString();

            foreach (Reflector.CodeModel.IAssemblyReference ar in m.AssemblyReferences)
            {
                if (ar.ToString() == anrName)
                {
                    return(ar);
                }
            }
            return(null);
        }
コード例 #8
0
        public override AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            Console.WriteLine(name.ToString());
            AssemblyDefinition assembly = null;

            try { assembly = m_assemblyResolver.Resolve(name); } catch (AssemblyResolutionException) {
                foreach (string assPath in m_assemblyPaths)
                {
                    string [] libraries  = Directory.GetFiles(assPath, "*.dll", SearchOption.AllDirectories);
                    string    missingLib = libraries.Where(lib => lib.Contains(name.Name)).SingleOrDefault();
                    Console.WriteLine(missingLib != null ? "found" : "not found");
                    if (missingLib != null)
                    {
                        return(AssemblyDefinition.ReadAssembly(missingLib));
                    }
                }
            }

            return(assembly);
        }
コード例 #9
0
        public override AssemblyDefinition Resolve(AssemblyNameReference reference, ReaderParameters parameters)
        {
            var name = reference.Name;

            AssemblyDefinition assembly;

            if (cache.TryGetValue(name, out assembly))
            {
                return(assembly);
            }

            if (CommandLineAssemblies != null && CommandLineAssemblies.Count > 0)
            {
                string cmdasm = CommandLineAssemblies.Find(t => {
                    if (String.IsNullOrEmpty(t))
                    {
                        return(false);
                    }
                    return(String.Compare(name, Path.GetFileNameWithoutExtension(t), StringComparison.Ordinal) == 0);
                });
                assembly = String.IsNullOrEmpty(cmdasm) ? null : Load(cmdasm);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            if (ArchDirectory != null)
            {
                assembly = SearchDirectory(name, ArchDirectory);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            assembly = SearchDirectory(name, FrameworkDirectory);
            if (assembly != null)
            {
                return(assembly);
            }

            var pclPath = Path.Combine(FrameworkDirectory, "Facades");

            if (Directory.Exists(pclPath))
            {
                assembly = SearchDirectory(name, pclPath);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            assembly = SearchDirectory(name, RootDirectory, ".exe");
            if (assembly != null)
            {
                return(assembly);
            }

            if (!string.IsNullOrEmpty(GlobalAssemblyCache))
            {
                var gac_folder = new StringBuilder()
                                 .Append(reference.Version)
                                 .Append("__");

                for (int i = 0; i < reference.PublicKeyToken.Length; i++)
                {
                    gac_folder.Append(reference.PublicKeyToken [i].ToString("x2"));
                }

                var gac_path = Path.Combine(GlobalAssemblyCache, reference.Name, gac_folder.ToString(), reference.Name + ".dll");
                if (File.Exists(gac_path))
                {
                    if (Driver.IsUnifiedFullXamMacFramework)
                    {
                        ErrorHelper.Warning(176, Errors.MX0176, reference.ToString(), gac_path);
                    }
                    return(Load(gac_path));
                }
            }

            if (SystemFrameworkDirectories?.Length > 0)
            {
                foreach (var dir in SystemFrameworkDirectories)
                {
                    assembly = SearchDirectory(reference.Name, dir);
                    if (assembly != null)
                    {
                        if (Driver.IsUnifiedFullXamMacFramework)
                        {
                            ErrorHelper.Warning(176, Errors.MX0176, reference.ToString(), assembly.MainModule.FileName);
                        }
                        return(assembly);
                    }
                }
            }

            return(null);
        }
コード例 #10
0
ファイル: AssemblyResolver.cs プロジェクト: lvyitian/Alex
        private bool TryFindAssemblyPath(AssemblyNameReference name, string rootPath, out string resultingPath)
        {
            AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(rootPath);
            var p = resolver.ResolveAssemblyToPath(new AssemblyName(name.ToString()));

            if (p != null)
            {
                resultingPath = p;
                return(true);
            }

            string dllName = name.Name + ".dll";

            var assemblyLocation = rootPath;

            string file = Path.Combine(assemblyLocation, dllName);

            string result = null;

            if (CompareFileToAssemblyName(file, name) == FileAssemblyComparisonResult.Match)
            {
                result = file;
            }
            else
            {
                foreach (var path in AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic).Select(x => x.Location))
                {
                    file = Path.Combine(path, dllName);
                    if (CompareFileToAssemblyName(file, name) == FileAssemblyComparisonResult.Match)
                    {
                        result = file;

                        break;
                    }
                }

                /*string lastPath = _lastPath;
                 * if (!string.IsNullOrEmpty(lastPath))
                 * {
                 *      file = Path.Combine(lastPath, dllName);
                 *      if (CompareFileToAssemblyName(file, name) == FileAssemblyComparisonResult.Match)
                 *      {
                 *              result = file;
                 *      }
                 * }
                 */
                string executingPath         = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string callingAssembliesPath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);

                if (result == null && executingPath != null)
                {
                    file = Path.Combine(executingPath, dllName);
                    if (File.Exists(Path.Combine(executingPath, dllName)))
                    {
                        result = file;
                    }
                }

                if (result == null && callingAssembliesPath != null)
                {
                    file = Path.Combine(callingAssembliesPath, dllName);
                    if (CompareFileToAssemblyName(file, name) == FileAssemblyComparisonResult.Match)
                    {
                        result = file;
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(result))
            {
                resultingPath = default(string);
                return(false);
            }
            else
            {
                resultingPath = result;
                return(true);
            }
        }
コード例 #11
0
        private LoadedAssembly LookupReferencedAssemblyInternal(AssemblyNameReference fullName, bool isWinRT)
        {
            string GetName(AssemblyNameReference name) => isWinRT ? name.Name : name.FullName;

            string         file;
            LoadedAssembly asm;

            lock (loadingAssemblies) {
                foreach (LoadedAssembly loaded in assemblyList.GetAssemblies())
                {
                    var asmDef = loaded.GetAssemblyDefinitionOrNull();
                    if (asmDef != null && GetName(fullName).Equals(GetName(asmDef.Name), StringComparison.OrdinalIgnoreCase))
                    {
                        LoadedAssemblyReferencesInfo.AddMessageOnce(fullName.ToString(), MessageKind.Info, "Success - Found in Assembly List");
                        return(loaded);
                    }
                }

                if (isWinRT)
                {
                    file = Path.Combine(Environment.SystemDirectory, "WinMetadata", fullName.Name + ".winmd");
                }
                else
                {
                    var resolver = new MyUniversalResolver(this)
                    {
                        TargetFramework = GetTargetFrameworkIdAsync().Result
                    };
                    file = resolver.FindAssemblyFile(fullName);
                }

                foreach (LoadedAssembly loaded in assemblyList.GetAssemblies())
                {
                    if (loaded.FileName.Equals(file, StringComparison.OrdinalIgnoreCase))
                    {
                        return(loaded);
                    }
                }

                if (file != null && loadingAssemblies.TryGetValue(file, out asm))
                {
                    return(asm);
                }

                if (assemblyLoadDisableCount > 0)
                {
                    return(null);
                }

                if (file != null)
                {
                    LoadedAssemblyReferencesInfo.AddMessage(fullName.ToString(), MessageKind.Info, "Success - Loading from: " + file);
                    asm = new LoadedAssembly(assemblyList, file)
                    {
                        IsAutoLoaded = true
                    };
                }
                else
                {
                    LoadedAssemblyReferencesInfo.AddMessageOnce(fullName.ToString(), MessageKind.Error, "Could not find reference: " + fullName);
                    return(null);
                }
                loadingAssemblies.Add(file, asm);
            }
            App.Current.Dispatcher.BeginInvoke((Action) delegate() {
                lock (assemblyList.assemblies) {
                    assemblyList.assemblies.Add(asm);
                }
                lock (loadingAssemblies) {
                    loadingAssemblies.Remove(file);
                }
            });
            return(asm);
        }
コード例 #12
0
        /// <summary>
        /// this is fully reentrant and multithreading capable, but will itself block.
        /// </summary>
        private AssemblyDefinition Load(AssemblyNameReference name, string assemblyFilename, ReaderParameters parameters)
        {
            AssemblyDefinition ret = null;
            TaskCompletionSource <AssemblyDefinition> loadingTaskSource = new TaskCompletionSource <AssemblyDefinition>();

            bool nameRegistered = false, filenameRegistered = false;

            try
            {
                // First, make sure we are the only one loading.
                while (true)
                {
                    Task <AssemblyDefinition> loadingTask;
                    if (name != null && !nameRegistered)
                    {
                        if (loadingTasks.TryGetValue(name.Name, out loadingTask))
                        {
                            ret = loadingTask.Result;
                            return(ret);
                        }

                        if (!loadingTasks.TryAdd(name.Name, loadingTaskSource.Task))
                        {
                            continue;
                        }
                        nameRegistered = true;
                    }

                    if (loadingTasks.TryAdd(assemblyFilename, loadingTaskSource.Task))
                    {
                        filenameRegistered = true;
                        break;
                    }
                    if (loadingTasks.TryGetValue(assemblyFilename, out loadingTask))
                    {
                        if (loadingTask == loadingTaskSource.Task)
                        {
                            break;
                        }

                        ret = loadingTask.Result;
                        return(ret);
                    }
                }

                // now check if it has already been loaded.
                if (name != null)
                {
                    if (referencesByName.TryGetValue(name.Name, out ret))
                    {
                        return(ret);
                    }
                }

                ret = fileNamesByAssembly.FirstOrDefault(v => v.Value.Equals(assemblyFilename, StringComparison.InvariantCultureIgnoreCase)).Key;
                if (ret != null)
                {
                    return(ret);
                }

                // now load the assembly.
                Console.WriteLine("Loading {0}...", Path.GetFileName(assemblyFilename));

                AssemblyDefinition assm = AssemblyDefinition.ReadAssembly(assemblyFilename, parameters);

                VerifyFrameworkAssembly(assm, assemblyFilename);

                // have to use a lock to update both data structures at the same time.
                lock (referencesByName)
                {
                    // now check again by the assembly name if it has been loaded before.
                    // This can happen if we were only provided a file name
                    if (!referencesByName.TryAdd(assm.Name.Name, assm))
                    {
                        ret = referencesByName[assm.Name.Name];
                        return(ret);
                    }

                    fileNamesByAssembly[assm] = assemblyFilename;
                }

                ret = assm;

                // now notify any listeners. note that if we were called on multiple threads
                // these might be called reentrantly as well.

                if (assemblyLoaded != null)
                {
                    assemblyLoaded(ret);
                }

                if (classLoader != null)
                {
                    classLoader.LoadAssembly(ret);
                }

                // done.
                return(ret);
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
#endif
                // Log the error
                var assemblyName = name == null?assemblyFilename:name.ToString();
                DLog.Error(DContext.CompilerAssemblyResolver, "Failed to load assembly {0}: {1}", assemblyName, ex.Message);

                // Pass the error on
                var exn = new AssemblyResolutionException(name ?? new AssemblyNameDefinition(assemblyFilename, new Version()));
                loadingTaskSource.SetException(exn);
                throw exn;
            }
            finally
            {
                loadingTaskSource.TrySetResult(ret);

                // cleanup our registrations
                Task <AssemblyDefinition> ignore;
                if (nameRegistered)
                {
                    loadingTasks.TryRemove(name.Name, out ignore);
                }
                if (filenameRegistered)
                {
                    loadingTasks.TryRemove(assemblyFilename, out ignore);
                }
            }
        }