Ejemplo n.º 1
0
        public static BindOperation Reflection_CustomALC()
        {
            CustomALC  alc       = new CustomALC(nameof(Reflection_CustomALC));
            Type       testClass = LoadTestClassInALC(alc);
            MethodInfo method    = testClass.GetMethod(nameof(GetDependentAssemblyType), BindingFlags.NonPublic | BindingFlags.Static);
            Type       t         = (Type)method.Invoke(null, new object[0]);

            return(new BindOperation()
            {
                AssemblyName = new AssemblyName(DependentAssemblyName),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = testClass.Assembly.GetName(),
                RequestingAssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = t.Assembly.GetName(),
                ResultAssemblyPath = t.Assembly.Location,
                Cached = false,
            });
        }
Ejemplo n.º 2
0
        public static BindOperation LoadFromStream()
        {
            var       executingAssembly = Assembly.GetExecutingAssembly();
            Stream    stream            = File.OpenRead(executingAssembly.Location);
            CustomALC alc = new CustomALC(nameof(LoadFromStream));
            Assembly  asm = alc.LoadFromStream(stream);

            return(new BindOperation()
            {
                AssemblyName = executingAssembly.GetName(),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = CoreLibName,
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
Ejemplo n.º 3
0
        public static BindOperation JITLoad_CustomALC()
        {
            CustomALC  alc       = new CustomALC(nameof(JITLoad_CustomALC));
            Type       testClass = LoadTestClassInALC(alc);
            MethodInfo method    = testClass.GetMethod(nameof(UseDependentAssembly), BindingFlags.NonPublic | BindingFlags.Static);
            Assembly   asm       = (Assembly)method.Invoke(null, new object[0]);

            return(new BindOperation()
            {
                AssemblyName = asm.GetName(),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = testClass.Assembly.GetName(),
                RequestingAssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            });
        }
        public static BindOperation LoadFromAssemblyPath()
        {
            CustomALC alc = new CustomALC(nameof(LoadFromAssemblyPath));
            var executingAssembly = Assembly.GetExecutingAssembly();
            Assembly asm = alc.LoadFromAssemblyPath(executingAssembly.Location);

            return new BindOperation()
            {
                AssemblyName = executingAssembly.GetName(),
                AssemblyPath = executingAssembly.Location,
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = CoreLibName,
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false
            };
        }
            private Assembly OnAppDomainAssemblyResolve(object sender, ResolveEventArgs args)
            {
                var      assemblyName  = new AssemblyName(args.Name);
                var      customContext = new CustomALC(nameof(OnAppDomainAssemblyResolve));
                Assembly asm           = ResolveAssembly(customContext, assemblyName);
                var      invocation    = new HandlerInvocation()
                {
                    AssemblyName = assemblyName,
                    HandlerName  = nameof(OnAppDomainAssemblyResolve),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
        public static BindOperation SatelliteAssembly_NotFound()
        {
            string       cultureName  = "en-GB";
            AssemblyName assemblyName = new AssemblyName($"{SubdirectoryAssemblyName}.resources");

            assemblyName.CultureInfo = new CultureInfo(cultureName);
            CustomALC alc = new CustomALC(nameof(SatelliteAssembly_CultureSubdirectory));

            alc.LoadFromAssemblyPath(Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName));
            Assert.Throws <FileNotFoundException>(() => alc.LoadFromAssemblyName(assemblyName));

            var probedPaths = new List <ProbedPath>()
            {
                new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, cultureName, Helpers.GetSubdirectoryPath()),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = COR_E_FILENOTFOUND
                }
            };

            // On Linux (case-sensitive), the path with a lower-case culture name should also be probed
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                probedPaths.Add(new ProbedPath()
                {
                    FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.SatelliteSubdirectory, assemblyName.Name, cultureName.ToLowerInvariant(), Helpers.GetSubdirectoryPath()),
                    Source   = ProbedPath.PathSource.SatelliteSubdirectory,
                    Result   = COR_E_FILENOTFOUND
                });
            }

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = false,
                Cached = false,
                ProbedPaths = probedPaths
            });
        }
Ejemplo n.º 7
0
        public static BindOperation AssemblyLoadContextLoad_Exception()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            var       assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextLoad), true /*throwOnLoad*/);

            Assert.Throws <FileLoadException, Exception>(() => alc.LoadFromAssemblyName(assemblyName));

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = false,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, $"Exception on Load in '{alc.ToString()}'")
                }
            });
        }
        public static BindOperation ContextualReflection_DefaultToCustomALC()
        {
            Type t;
            CustomALC alc = new CustomALC(nameof(ContextualReflection_DefaultToCustomALC));
            using (alc.EnterContextualReflection())
            {
                t = GetDependentAssemblyType();
            }

            return new BindOperation()
            {
                AssemblyName = new AssemblyName(DependentAssemblyName),
                AssemblyLoadContext = alc.ToString(),
                RequestingAssembly = Assembly.GetExecutingAssembly().GetName(),
                RequestingAssemblyLoadContext = DefaultALC,
                Success = true,
                ResultAssemblyName = t.Assembly.GetName(),
                ResultAssemblyPath = t.Assembly.Location,
                Cached = false,
            };
        }
        public static BindOperation AssemblyLoadContextResolving_NameMismatch()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextResolving_NameMismatch));

            using (var handlers = new Handlers(HandlerReturn.NameMismatch, alc))
            {
                Assert.Throws <FileLoadException>(() => alc.LoadFromAssemblyName(assemblyName));

                Assert.AreEqual(1, handlers.Invocations.Count);
                Assert.AreEqual(1, handlers.Binds.Count);
                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = false,
                    Cached = false,
                    AssemblyLoadContextResolvingHandlers = handlers.Invocations,
                    NestedBinds = handlers.Binds
                });
            }
        }
        public static BindOperation AssemblyInAppPath()
        {
            AssemblyName assemblyName = new AssemblyName(DependentAssemblyName);
            CustomALC    alc          = new CustomALC(nameof(AssemblyInAppPath));
            Assembly     asm          = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ProbedPaths = new List <ProbedPath>()
                {
                    new ProbedPath()
                    {
                        FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.AppNativeImagePaths, assemblyName.Name, isExe: false),
                        Source = ProbedPath.PathSource.AppNativeImagePaths,
                        Result = COR_E_FILENOTFOUND
                    },
                    new ProbedPath()
                    {
                        FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.AppNativeImagePaths, assemblyName.Name, isExe: true),
                        Source = ProbedPath.PathSource.AppNativeImagePaths,
                        Result = COR_E_FILENOTFOUND
                    },
                    new ProbedPath()
                    {
                        FilePath = Helpers.GetProbingFilePath(ProbedPath.PathSource.AppPaths, assemblyName.Name, isExe: false),
                        Source = ProbedPath.PathSource.AppPaths,
                        Result = S_OK
                    }
                }
            });
        }
Ejemplo n.º 11
0
        public static BindOperation DefaultAssemblyLoadContextFallback()
        {
            var       assemblyName = new AssemblyName("System.Xml");
            CustomALC alc          = new CustomALC(nameof(DefaultAssemblyLoadContextFallback));
            Assembly  asm          = alc.LoadFromAssemblyName(assemblyName);

            return(new BindOperation()
            {
                AssemblyName = assemblyName,
                AssemblyLoadContext = alc.ToString(),
                Success = true,
                ResultAssemblyName = asm.GetName(),
                ResultAssemblyPath = asm.Location,
                Cached = false,
                ResolutionAttempts = new List <ResolutionAttempt>()
                {
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
                    GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.Success, asm),
                    GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.Success, asm)
                }
            });
        }
        public static BindOperation AssemblyLoadContextResolving_LoadAssembly()
        {
            var       assemblyName = new AssemblyName(SubdirectoryAssemblyName);
            CustomALC alc          = new CustomALC(nameof(AssemblyLoadContextResolving_LoadAssembly));

            using (var handlers = new Handlers(HandlerReturn.RequestedAssembly, alc))
            {
                Assembly asm = alc.LoadFromAssemblyName(assemblyName);

                Assert.AreEqual(1, handlers.Invocations.Count);
                Assert.AreEqual(1, handlers.Binds.Count);
                return(new BindOperation()
                {
                    AssemblyName = assemblyName,
                    AssemblyLoadContext = alc.ToString(),
                    Success = true,
                    ResultAssemblyName = asm.GetName(),
                    ResultAssemblyPath = asm.Location,
                    Cached = false,
                    AssemblyLoadContextResolvingHandlers = handlers.Invocations,
                    NestedBinds = handlers.Binds
                });
            }
        }