コード例 #1
0
        public override Assembly LoadAssemblyFile(string fileName, bool isImplicitReference)
        {
            bool?has_extension = null;

            foreach (var path in paths)
            {
                var file = Path.Combine(path, fileName);
                if (compiler.Settings.DebugFlags > 0)
                {
                    Console.WriteLine("Probing assembly location `{0}'", file);
                }

                if (!File.Exists(file))
                {
                    if (!has_extension.HasValue)
                    {
                        has_extension = fileName.EndsWith(".dll", StringComparison.Ordinal) || fileName.EndsWith(".exe", StringComparison.Ordinal);
                    }

                    if (has_extension.Value)
                    {
                        continue;
                    }

                    file += ".dll";
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                }

                try {
                    using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                        using (RawModule module = domain.OpenRawModule(stream, file)) {
                            if (!module.IsManifestModule)
                            {
                                Error_AssemblyIsModule(fileName);
                                return(null);
                            }

                            //
                            // check whether the assembly can be actually imported without
                            // collision
                            //
                            var an = module.GetAssemblyName();
                            foreach (var entry in loaded_names)
                            {
                                var loaded_name = entry.Item1;
                                if (an.Name != loaded_name.Name)
                                {
                                    continue;
                                }

                                if (module.ModuleVersionId == entry.Item3.ManifestModule.ModuleVersionId)
                                {
                                    return(entry.Item3);
                                }

                                if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0)
                                {
                                    compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                    compiler.Report.SymbolRelatedToPreviousError(fileName);
                                    compiler.Report.Error(1704,
                                                          "An assembly with the same name `{0}' has already been imported. Consider removing one of the references or sign the assembly",
                                                          an.Name);
                                    return(null);
                                }

                                if ((an.Flags & AssemblyNameFlags.PublicKey) == (loaded_name.Flags & AssemblyNameFlags.PublicKey))
                                {
                                    compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                    compiler.Report.SymbolRelatedToPreviousError(fileName);
                                    compiler.Report.Error(1703,
                                                          "An assembly `{0}' with the same identity has already been imported. Consider removing one of the references",
                                                          an.Name);
                                    return(null);
                                }
                            }

                            if (compiler.Settings.DebugFlags > 0)
                            {
                                Console.WriteLine("Loading assembly `{0}'", fileName);
                            }

                            var assembly = domain.LoadAssembly(module);
                            if (assembly != null)
                            {
                                loaded_names.Add(Tuple.Create(an, fileName, assembly));
                            }

                            return(assembly);
                        }
                    }
                } catch (Exception e) {
                    if (compiler.Settings.DebugFlags > 0)
                    {
                        Console.WriteLine("Exception during loading: {0}'", e.ToString());
                    }

                    if (!isImplicitReference)
                    {
                        Error_FileCorrupted(file);
                    }

                    return(null);
                }
            }

            if (!isImplicitReference)
            {
                Error_FileNotFound(fileName);
            }

            return(null);
        }
コード例 #2
0
        public override Assembly LoadAssemblyFile(string fileName)
        {
            bool?has_extension = null;

            foreach (var path in paths)
            {
                var file = Path.Combine(path, fileName);
                if (!File.Exists(file))
                {
                    if (!has_extension.HasValue)
                    {
                        has_extension = fileName.EndsWith(".dll", StringComparison.Ordinal) || fileName.EndsWith(".exe", StringComparison.Ordinal);
                    }

                    if (has_extension.Value)
                    {
                        continue;
                    }

                    file += ".dll";
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                }

                try
                {
                    using (RawModule module = domain.OpenRawModule(file))
                    {
                        if (!module.IsManifestModule)
                        {
                            Error_AssemblyIsModule(fileName);
                            return(null);
                        }

                        //
                        // check whether the assembly can be actually imported without
                        // collision
                        //
                        var an = module.GetAssemblyName();
                        foreach (var entry in loaded_names)
                        {
                            var loaded_name = entry.Item1;
                            if (an.Name != loaded_name.Name)
                            {
                                continue;
                            }

                            if (an.CodeBase == loaded_name.CodeBase)
                            {
                                return(null);
                            }

                            if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0)
                            {
                                compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                compiler.Report.SymbolRelatedToPreviousError(fileName);
                                compiler.Report.Error(1704,
                                                      "An assembly with the same name `{0}' has already been imported. Consider removing one of the references or sign the assembly",
                                                      an.Name);
                                return(null);
                            }

                            if ((an.Flags & AssemblyNameFlags.PublicKey) == (loaded_name.Flags & AssemblyNameFlags.PublicKey) && an.Version.Equals(loaded_name.Version))
                            {
                                compiler.Report.SymbolRelatedToPreviousError(entry.Item2);
                                compiler.Report.SymbolRelatedToPreviousError(fileName);
                                compiler.Report.Error(1703,
                                                      "An assembly with the same identity `{0}' has already been imported. Consider removing one of the references",
                                                      an.FullName);
                                return(null);
                            }
                        }

                        if (Report.DebugFlags > 0)
                        {
                            Console.WriteLine("Loading assembly `{0}'", fileName);
                        }

                        loaded_names.Add(Tuple.Create(an, fileName));
                        return(domain.LoadAssembly(module));
                    }
                }
                catch
                {
                    Error_FileCorrupted(file);
                    return(null);
                }
            }

            Error_FileNotFound(fileName);
            return(null);
        }