コード例 #1
0
ファイル: Assembly.cs プロジェクト: vscodewinodws/runtime
        private static Assembly?LoadFromResolveHandler(object?sender, ResolveEventArgs args)
        {
            Assembly?requestingAssembly = args.RequestingAssembly;

            if (requestingAssembly == null)
            {
                return(null);
            }

            // Requesting assembly for LoadFrom is always loaded in defaultContext - proceed only if that
            // is the case.
            if (AssemblyLoadContext.Default != AssemblyLoadContext.GetLoadContext(requestingAssembly))
            {
                return(null);
            }

            // Get the path where requesting assembly lives and check if it is in the list
            // of assemblies for which LoadFrom was invoked.
            string requestorPath = Path.GetFullPath(requestingAssembly.Location);

            if (string.IsNullOrEmpty(requestorPath))
            {
                return(null);
            }

            lock (s_loadFromAssemblyList)
            {
                // If the requestor assembly was not loaded using LoadFrom, exit.
                if (!s_loadFromAssemblyList.Contains(requestorPath))
                {
#if CORECLR
                    if (AssemblyLoadContext.IsTracingEnabled())
                    {
                        AssemblyLoadContext.TraceAssemblyLoadFromResolveHandlerInvoked(args.Name, false, requestorPath, null);
                    }
#endif // CORECLR
                    return(null);
                }
            }

            // Requestor assembly was loaded using loadFrom, so look for its dependencies
            // in the same folder as it.
            // Form the name of the assembly using the path of the assembly that requested its load.
            AssemblyName requestedAssemblyName = new AssemblyName(args.Name !);
            string       requestedAssemblyPath = Path.Combine(Path.GetDirectoryName(requestorPath) !, requestedAssemblyName.Name + ".dll");
#if CORECLR
            if (AssemblyLoadContext.IsTracingEnabled())
            {
                AssemblyLoadContext.TraceAssemblyLoadFromResolveHandlerInvoked(args.Name, true, requestorPath, requestedAssemblyPath);
            }
#endif // CORECLR
            try
            {
                // Load the dependency via LoadFrom so that it goes through the same path of being in the LoadFrom list.
                return(Assembly.LoadFrom(requestedAssemblyPath));
            }
            catch (FileNotFoundException)
            {
                // Catch FileNotFoundException when attempting to resolve assemblies via this handler to account for missing assemblies.
                return(null);
            }
        }