void TestAssemblyLoad(bool isolated, Func <string, Assembly> assemblyLoad) { TestResolveMissingAssembly(isolated, (string assemblyName) => assemblyLoad(assemblyName)); using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { Assembly assembly = assemblyLoad("ContextualReflection"); Assert.AreEqual(isolated ? alc : AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(assembly)); Assembly depends = assemblyLoad("ContextualReflectionDependency"); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(depends)); } using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { Assembly assembly = assemblyLoad("ContextualReflection"); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(assembly)); Assembly depends = assemblyLoad("ContextualReflectionDependency"); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(depends)); } using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { Assembly assembly = assemblyLoad("ContextualReflection"); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(assembly)); Assembly depends = assemblyLoad("ContextualReflectionDependency"); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(depends)); } }
void VerifyContextualReflectionProxy() { Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { Assert.AreEqual(AssemblyLoadContext.Default, ConntextualReflectionProxy.CurrentContextualReflectionContext); using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); using (ConntextualReflectionProxy.EnterContextualReflection(alcAssembly)) { Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); } Assert.AreEqual(AssemblyLoadContext.Default, ConntextualReflectionProxy.CurrentContextualReflectionContext); } Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); }
void TestResolveMissingAssembly(bool isolated, Action <string> action, bool skipNullIsolated = false) { using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { TestResolve.Assert(ResolveEvents.ExpectedEvent, () => action("TestDefaultLoad")); if (!skipNullIsolated) { TestResolve.Assert(isolated ? ResolveEvents.ExpectedEvent : ResolveEvents.NoEvent, () => action("TestIsolatedLoad")); } } using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { TestResolve.Assert(ResolveEvents.ExpectedEvent, () => action("TestDefaultLoad")); TestResolve.Assert(ResolveEvents.NoEvent, () => action("TestIsolatedLoad")); } using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { TestResolve.Assert(ResolveEvents.ExpectedEvent, () => action("TestDefaultLoad")); TestResolve.Assert(ResolveEvents.ExpectedEvent, () => action("TestIsolatedLoad")); } }
void TestActivatorCreateInstance(bool isolated) { TestResolveMissingAssembly(isolated, (string assemblyName) => Activator.CreateInstance(assemblyName, "MyType")); TestResolveMissingAssembly(isolated, (string assemblyName) => Activator.CreateInstance("System.Private.CoreLib", string.Format("System.Collections.Generic.List`1[[MyType, {0}]]", assemblyName)), skipNullIsolated: true); TestResolveMissingAssembly(isolated, (string assemblyName) => Activator.CreateInstance("ContextualReflection", string.Format("ContextualReflectionTest.AGenericClass`1[[MyType, {0}]]", assemblyName))); Assembly assembly = Assembly.GetExecutingAssembly(); using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]"); Type g = objectHandle.Unwrap().GetType(); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { ObjectHandle objectHandle = Activator.CreateInstance("ContextualReflection", "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = assembly; Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { Assembly expectedAssembly = alcAssembly; Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; ObjectHandle objectHandle = Activator.CreateInstance(mscorlib.GetName().Name, "System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type m = objectHandle.Unwrap().GetType(); Assert.IsNotNull(m); Assert.AreEqual(mscorlib, m.Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]"); Type g = objectHandle.Unwrap().GetType(); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = defaultAssembly; Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { ObjectHandle objectHandle = Activator.CreateInstance("ContextualReflection", "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = defaultAssembly; Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; ObjectHandle objectHandle = Activator.CreateInstance(mscorlib.GetName().Name, "System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type m = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = mscorlib; Assert.IsNotNull(m); Assert.AreEqual(expectedAssembly, m.Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]"); Type g = objectHandle.Unwrap().GetType(); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { ObjectHandle objectHandle = Activator.CreateInstance(null, "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = alcAssembly; Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { ObjectHandle objectHandle = Activator.CreateInstance("ContextualReflection", "ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type g = objectHandle.Unwrap().GetType(); Assembly expectedAssembly = alcAssembly; Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; ObjectHandle objectHandle = Activator.CreateInstance(mscorlib.GetName().Name, "System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]"); Type m = objectHandle.Unwrap().GetType(); Assert.IsNotNull(m); Assert.AreEqual(mscorlib, m.Assembly); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } }
void TestAssemblyGetType(bool isolated) { Assembly assembly = Assembly.GetExecutingAssembly(); TestResolveMissingAssembly(isolated, (string assemblyName) => assembly.GetType(string.Format("ContextualReflectionTest.AGenericClass`1[[MyType, {0}]]", assemblyName))); using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]", throwOnError: false); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; Type m = mscorlib.GetType("System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assembly expectedAssembly = mscorlib; Assert.IsNotNull(m); Assert.AreEqual(expectedAssembly, m.Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]", throwOnError: false); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assembly expectedAssembly = defaultAssembly; Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; Type m = mscorlib.GetType("System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assembly expectedAssembly = mscorlib; Assert.IsNotNull(m); Assert.AreEqual(expectedAssembly, m.Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program]]", throwOnError: false); Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(assembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(Assembly.GetExecutingAssembly(), g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); } { Type g = assembly.GetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assembly expectedAssembly = alcAssembly; Assert.IsNotNull(g); Assert.AreEqual(assembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); } { Assembly mscorlib = typeof(System.Collections.Generic.List <string>).Assembly; Type m = mscorlib.GetType("System.Collections.Generic.List`1[[ContextualReflectionTest.Program, ContextualReflection]]", throwOnError: false); Assembly expectedAssembly = mscorlib; Assert.IsNotNull(m); Assert.AreEqual(expectedAssembly, m.Assembly); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(m.GenericTypeArguments[0].Assembly)); } } }
void TestTypeGetType(bool isolated, Func <string, System.Type> typeGetType) { TestResolveMissingAssembly(isolated, (string assemblyName) => typeGetType(string.Format("MyType, {0}", assemblyName))); using (ConntextualReflectionProxy.EnterContextualReflection((Assembly)null)) { { Type p = typeGetType("ContextualReflectionTest.Program"); Assembly expectedAssembly = Assembly.GetExecutingAssembly(); Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(typeof(Program), p); } { Type p = typeGetType("ContextualReflectionTest.Program, ContextualReflection"); Assembly expectedAssembly = Assembly.GetExecutingAssembly(); Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(typeof(Program), p); } { Type g = typeGetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]], ContextualReflection"); Assembly expectedAssembly = Assembly.GetExecutingAssembly(); Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(typeof(Program), g.GenericTypeArguments[0]); Assert.AreEqual(isolated ? alc : AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(g.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default)) { { Type p = typeGetType("ContextualReflectionTest.Program"); Assembly expectedAssembly = Assembly.GetExecutingAssembly(); Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(typeof(Program), p); } { Type p = typeGetType("ContextualReflectionTest.Program, ContextualReflection"); Assembly expectedAssembly = defaultAssembly; Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(p.Assembly)); } { Type g = typeGetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]], ContextualReflection"); Assembly expectedAssembly = defaultAssembly; Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(g.Assembly)); Assert.AreEqual(AssemblyLoadContext.Default, AssemblyLoadContext.GetLoadContext(g.GenericTypeArguments[0].Assembly)); } } using (ConntextualReflectionProxy.EnterContextualReflection(alc)) { { Type p = typeGetType("ContextualReflectionTest.Program"); Assembly expectedAssembly = Assembly.GetExecutingAssembly(); Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(typeof(Program), p); } { Type p = typeGetType("ContextualReflectionTest.Program, ContextualReflection"); Assembly expectedAssembly = alcAssembly; Assert.IsNotNull(p); Assert.AreEqual(expectedAssembly, p.Assembly); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(p.Assembly)); } { Type g = typeGetType("ContextualReflectionTest.AGenericClass`1[[ContextualReflectionTest.Program, ContextualReflection]], ContextualReflection"); Assembly expectedAssembly = alcAssembly; Assert.IsNotNull(g); Assert.AreEqual(expectedAssembly, g.Assembly); Assert.AreEqual(expectedAssembly, g.GenericTypeArguments[0].Assembly); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(g.Assembly)); Assert.AreEqual(alc, AssemblyLoadContext.GetLoadContext(g.GenericTypeArguments[0].Assembly)); } } }
void VerifyBadContextualReflectionUsage() { Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); { IDisposable alcScope = ConntextualReflectionProxy.EnterContextualReflection(alc); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); alcScope.Dispose(); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); { IDisposable alcScope = ConntextualReflectionProxy.EnterContextualReflection(alc); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); alcScope.Dispose(); alcScope.Dispose(); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); { IDisposable alcScope = ConntextualReflectionProxy.EnterContextualReflection(alc); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); IDisposable defaultScope = ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default); Assert.AreEqual(AssemblyLoadContext.Default, ConntextualReflectionProxy.CurrentContextualReflectionContext); defaultScope.Dispose(); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); alcScope.Dispose(); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); { IDisposable alcScope = ConntextualReflectionProxy.EnterContextualReflection(alc); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); IDisposable defaultScope = ConntextualReflectionProxy.EnterContextualReflection(AssemblyLoadContext.Default); Assert.AreEqual(AssemblyLoadContext.Default, ConntextualReflectionProxy.CurrentContextualReflectionContext); alcScope.Dispose(); Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); defaultScope.Dispose(); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); alcScope.Dispose(); } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); { IDisposable alcScope = ConntextualReflectionProxy.EnterContextualReflection(alc); Assert.AreEqual(alc, ConntextualReflectionProxy.CurrentContextualReflectionContext); try { IDisposable defaultScope = ConntextualReflectionProxy.EnterContextualReflection((Assembly)null); Assert.AreEqual(null, ConntextualReflectionProxy.CurrentContextualReflectionContext); throw new InvalidOperationException(); } catch { } } Assert.IsNull(ConntextualReflectionProxy.CurrentContextualReflectionContext); }