Beispiel #1
0
        internal void RegisterAssemblyPaths(IList <string> assemblyPaths)
        {
#if !NETFRAMEWORK
            Context.AssemblyPaths = assemblyPaths;
#else
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                Version version = null;

                var separatorIndex = args.Name.IndexOf(",", StringComparison.Ordinal);

                // Fetch assembly name(Newtonsoft.Json) from args value like below
                // Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
                var assemblyName = separatorIndex > 0 ? args.Name.Substring(0, separatorIndex) : args.Name;

                if (separatorIndex > 0)
                {
                    // Fetch assembly version(11.0.0.0) from args value like below
                    // Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
                    separatorIndex = args.Name.IndexOf("=", separatorIndex, StringComparison.Ordinal);
                    if (separatorIndex > 0)
                    {
                        var endIndex = args.Name.IndexOf(",", separatorIndex, StringComparison.Ordinal);
                        if (endIndex > 0)
                        {
                            var parts = args.Name
                                        .Substring(separatorIndex + 1, endIndex - separatorIndex - 1)
                                        .Split('.');

                            version = new Version(int.Parse(parts[0]),
                                                  int.Parse(parts[1]),
                                                  int.Parse(parts[2]),
                                                  int.Parse(parts[3]));
                        }
                    }
                }

                var existingAssembly = AppDomain.CurrentDomain.GetAssemblies()
                                       .FirstOrDefault(a => a.FullName == args.Name);
                if (existingAssembly != null)
                {
                    return(existingAssembly);
                }

                if (version != null)
                {
                    var assemblyByVersion = AssemblyLoadUtility.TryLoadByNameOrVersion(
                        assemblyPaths,
                        assemblyName,
                        version.Major + "." + version.Minor + "." + version.Build + ".");

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

                    assemblyByVersion = AssemblyLoadUtility.TryLoadByNameOrVersion(
                        assemblyPaths,
                        assemblyName,
                        version.Major + "." + version.Minor + ".");

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

                    assemblyByVersion =
                        AssemblyLoadUtility.TryLoadByNameOrVersion(assemblyPaths, assemblyName, version.Major + ".");

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

                var assembly = AssemblyLoadUtility.TryLoadByNameOrVersion(assemblyPaths, assemblyName);
                if (assembly != null)
                {
                    return(assembly);
                }

                return(null);
            };
#endif
        }
Beispiel #2
0
        protected override Assembly Load(AssemblyName args)
        {
            if (Assemblies.ContainsKey(args.Name))
            {
                return(Assemblies[args.Name]);
            }

            var separatorIndex = args.Name.IndexOf(",", StringComparison.Ordinal);
            var assemblyName   = separatorIndex > 0 ? args.Name.Substring(0, separatorIndex) : args.Name;

            var assembly = TryLoadExistingAssemblyName(args.FullName);

            if (assembly != null)
            {
                Assemblies[args.Name] = assembly;
                return(assembly);
            }

            // Try to load assembly using version.
            var version = args.Version;

            if (version != null)
            {
                var assemblyByVersion = TryLoadByVersion(AssemblyPaths, assemblyName, version.Major + "." + version.Minor + "." + version.Build + ".");
                if (assemblyByVersion != null)
                {
                    Assemblies[args.Name] = assemblyByVersion;
                    return(assemblyByVersion);
                }

                assemblyByVersion = TryLoadByVersion(AssemblyPaths, assemblyName, version.Major + "." + version.Minor + ".");
                if (assemblyByVersion != null)
                {
                    Assemblies[args.Name] = assemblyByVersion;
                    return(assemblyByVersion);
                }

                assemblyByVersion = TryLoadByVersion(AssemblyPaths, assemblyName, version.Major + ".");
                if (assemblyByVersion != null)
                {
                    Assemblies[args.Name] = assemblyByVersion;
                    return(assemblyByVersion);
                }
            }

            // Try to load assembly using full name, which includes version too.
            assembly = TryLoadByAssemblyName(args.FullName);
            if (assembly != null)
            {
                Assemblies[args.Name] = assembly;
                return(assembly);
            }

            // Try to load assembly using short assembly name.
            assembly = AssemblyLoadUtility.TryLoadByNameOrVersion(AssemblyPaths, assemblyName);
            if (assembly != null)
            {
                Assemblies[args.Name] = assembly;
                return(assembly);
            }

            Assemblies[args.Name] = TryLoadByAssemblyName(assemblyName);
            return(Assemblies[args.Name]);
        }