Beispiel #1
0
        internal static IReadOnlyList <AssemblyReference> GetRelatedAssemblyReferences(
            Assembly assembly,
            Func <string, bool> fileExists,
            Func <string, Assembly> loadFile
            )
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            if (assembly.IsDynamic || assembly.ReflectionOnly || string.IsNullOrEmpty(assembly.CodeBase))
            {
                return(Array.Empty <AssemblyReference>());
            }

            var attributes = assembly.GetCustomAttributes <RelatedAssemblyAttribute>().ToArray();

            if (attributes.Length == 0)
            {
                return(Array.Empty <AssemblyReference>());
            }

            var assemblyName      = assembly.GetName().Name;
            var assemblyLocation  = GetAssemblyLocation(assembly);
            var assemblyDirectory = Path.GetDirectoryName(assemblyLocation);

            var relatedAssemblies = new List <AssemblyReference>(attributes.Length);

            foreach (var attribute in attributes)
            {
                if (string.Equals(assemblyName, attribute.AssemblyFileName, StringComparison.OrdinalIgnoreCase))
                {
                    throw RuntimeFailure.InvalidSelfRelatedAssembly(assemblyName);
                }

                relatedAssemblies.Add(new RelatedAssemblyReference(
                                          assembly.GetName(),
                                          assemblyDirectory,
                                          attribute.AssemblyFileName,
                                          fileExists,
                                          loadFile
                                          ));
            }

            return(relatedAssemblies);
        }