public unsafe static void TestCustomModifiers1() { using (TypeLoader tl = new TypeLoader("mscorlib")) { tl.Resolving += delegate(TypeLoader sender, AssemblyName name) { if (name.Name == "mscorlib") { return(tl.LoadFromStream(TestUtils.CreateStreamForCoreAssembly())); } return(null); }; Assembly a = tl.LoadFromByteArray(TestData.s_CustomModifiersImage); Type t = a.GetType("N", throwOnError: true); Type reqA = a.GetType("ReqA", throwOnError: true); Type reqB = a.GetType("ReqB", throwOnError: true); Type reqC = a.GetType("ReqC", throwOnError: true); Type optA = a.GetType("OptA", throwOnError: true); Type optB = a.GetType("OptB", throwOnError: true); Type optC = a.GetType("OptC", throwOnError: true); MethodInfo m = t.GetMethod("MyMethod"); ParameterInfo p = m.GetParameters()[0]; Type[] req = p.GetRequiredCustomModifiers(); Type[] opt = p.GetOptionalCustomModifiers(); Assert.Equal <Type>(new Type[] { reqA, reqB, reqC }, req); Assert.Equal <Type>(new Type[] { optA, optB, optC }, opt); TestUtils.AssertNewObjectReturnedEachTime(() => p.GetRequiredCustomModifiers()); TestUtils.AssertNewObjectReturnedEachTime(() => p.GetOptionalCustomModifiers()); } }
public static void LoadFromStreamNull() { using (TypeLoader tl = new TypeLoader()) { Assert.Throws <ArgumentNullException>(() => tl.LoadFromStream(null)); } }
public static void AssemblyDefinedTypeInfosReturnsDifferentObjectsEachTime() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromStream(TestUtils.CreateStreamForCoreAssembly()); TestUtils.AssertNewObjectReturnedEachTime <TypeInfo>(() => a.DefinedTypes); } }
public static void BadImageFormat() { using (TypeLoader tl = new TypeLoader()) { for (int i = 0; i < 100; i++) { Stream s = new MemoryStream(new byte[i]); Assert.Throws <BadImageFormatException>(() => tl.LoadFromStream(s)); } } }
public static void TypeLoaderApisAfterDispose() { TypeLoader tl = new TypeLoader(); tl.Dispose(); Assert.Throws <ObjectDisposedException>(() => tl.LoadFromAssemblyName(new AssemblyName("Foo"))); Assert.Throws <ObjectDisposedException>(() => tl.LoadFromAssemblyName("Foo")); Assert.Throws <ObjectDisposedException>(() => tl.LoadFromAssemblyPath("Foo")); Assert.Throws <ObjectDisposedException>(() => tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage)); Assert.Throws <ObjectDisposedException>(() => tl.LoadFromStream(new MemoryStream(TestData.s_SimpleAssemblyImage))); }
public static void LoadFromAssemblyName() { using (TypeLoader tl = new TypeLoader()) { Stream peStream = new MemoryStream(TestData.s_SimpleAssemblyImage); Assembly a = tl.LoadFromStream(peStream); Assert.NotNull(a); Assembly a1 = tl.LoadFromAssemblyName(TestData.s_SimpleAssemblyName); Assert.Equal(a, a1); Assembly a2 = tl.LoadFromAssemblyName(new AssemblyName(TestData.s_SimpleAssemblyName)); Assert.Equal(a, a2); } }
public static void LoadFromStreamMemorySimpleAssembly() { using (TypeLoader tl = new TypeLoader()) { Stream peStream = new MemoryStream(TestData.s_SimpleAssemblyImage); Assembly a = tl.LoadFromStream(peStream); Assert.NotNull(a); string fullName = a.GetName().FullName; Assert.Equal(TestData.s_SimpleAssemblyName, fullName); Guid mvid = a.ManifestModule.ModuleVersionId; Assert.Equal(TestData.s_SimpleAssemblyMvid, mvid); Assert.Equal(string.Empty, a.Location); } }
// Handler for TypeLoader.Resolving event responsible for assembly probing and loading. // TypeLoader will fire the Resolving event when an assembly or a dependent assembly is // required in response to a reflection request. Probing strategy that will // find all dependent DLLs, even those in other NuGet packages, must be implemented // here. The probing responsibility up to the consumer of System.Reflection.TypeLoader. private static System.Reflection.Assembly TypeLoaderResolvingHandler( System.Reflection.TypeLoader typeLoader, System.Reflection.AssemblyName assemblyName) { // Resolve is called at most once per assembly. foreach (var path in _cachedTypeLoaderReferencePaths) { string fullFilePath = path + "\\" + assemblyName.Name + ".dll"; if (System.IO.File.Exists(fullFilePath)) { // TODO: PBT Porting: Temporarily relax this restriction for testing. // Do not require a specific version, culture, or public key token // of the assembly. Update this block when testing is complete. return(_typeLoader.LoadFromStream(File.OpenRead(fullFilePath))); } } return(null); }
public static void LoadFromNonZeroPositionedStreamMemorySimpleAssembly() { using (TypeLoader tl = new TypeLoader()) { Stream peStream = new MemoryStream(TestData.s_SimpleAssemblyImage); peStream.Position = 1; // The TypeLoader takes ownership of the peStream. It will reset its position back to 0. Assembly a = tl.LoadFromStream(peStream); Assert.NotNull(a); string fullName = a.GetName().FullName; Assert.Equal(TestData.s_SimpleAssemblyName, fullName); Guid mvid = a.ManifestModule.ModuleVersionId; Assert.Equal(TestData.s_SimpleAssemblyMvid, mvid); Assert.Equal(string.Empty, a.Location); } }
public static void LoadFromStreamFileSimpleAssembly() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) { using (TypeLoader tl = new TypeLoader()) { Stream fs = File.OpenRead(tf.Path); Assembly a = tl.LoadFromStream(fs); Assert.NotNull(a); string fullName = a.GetName().FullName; Assert.Equal(TestData.s_SimpleAssemblyName, fullName); Guid mvid = a.ManifestModule.ModuleVersionId; Assert.Equal(TestData.s_SimpleAssemblyMvid, mvid); Assert.Equal(tf.Path, a.Location); } } }
public static void TestEHClauses() { using (TypeLoader tl = new TypeLoader()) { Assembly coreAssembly = tl.LoadFromStream(TestUtils.CreateStreamForCoreAssembly()); tl.Resolving += delegate(TypeLoader sender, AssemblyName name) { if (name.Name == "mscorlib") { return(coreAssembly); } return(null); }; Assembly a = tl.LoadFromByteArray(TestData.s_AssemblyWithEhClausesImage); Type gt = a.GetType("G`1", throwOnError: true); Type et = a.GetType("MyException`2", throwOnError: true); Type gtP0 = gt.GetGenericTypeParameters()[0]; Type etP0 = et.GetGenericTypeParameters()[0]; Type etP1 = et.GetGenericTypeParameters()[1]; { MethodInfo m = gt.GetMethod("Catch"); Type theM = m.GetGenericArguments()[0]; MethodBody body = m.GetMethodBody(); IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses; Assert.Equal(1, ehs.Count); ExceptionHandlingClause eh = ehs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Clause, eh.Flags); Assert.Equal(1, eh.TryOffset); Assert.Equal(15, eh.TryLength); Assert.Equal(16, eh.HandlerOffset); Assert.Equal(16, eh.HandlerLength); Assert.Throws <InvalidOperationException>(() => eh.FilterOffset); Assert.Equal(et.MakeGenericType(gtP0, theM), eh.CatchType); } { Type sysInt32 = coreAssembly.GetType("System.Int32", throwOnError: true); Type sysSingle = coreAssembly.GetType("System.Single", throwOnError: true); MethodInfo m = gt.MakeGenericType(sysInt32).GetMethod("Catch").MakeGenericMethod(sysSingle); MethodBody body = m.GetMethodBody(); IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses; Assert.Equal(1, ehs.Count); ExceptionHandlingClause eh = ehs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Clause, eh.Flags); Assert.Equal(1, eh.TryOffset); Assert.Equal(15, eh.TryLength); Assert.Equal(16, eh.HandlerOffset); Assert.Equal(16, eh.HandlerLength); Assert.Throws <InvalidOperationException>(() => eh.FilterOffset); Assert.Equal(et.MakeGenericType(sysInt32, sysSingle), eh.CatchType); } { MethodInfo m = gt.GetMethod("Finally"); MethodBody body = m.GetMethodBody(); IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses; Assert.Equal(1, ehs.Count); ExceptionHandlingClause eh = ehs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Finally, eh.Flags); Assert.Equal(1, eh.TryOffset); Assert.Equal(15, eh.TryLength); Assert.Equal(16, eh.HandlerOffset); Assert.Equal(14, eh.HandlerLength); Assert.Throws <InvalidOperationException>(() => eh.FilterOffset); Assert.Throws <InvalidOperationException>(() => eh.CatchType); } { MethodInfo m = gt.GetMethod("Fault"); MethodBody body = m.GetMethodBody(); IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses; Assert.Equal(1, ehs.Count); ExceptionHandlingClause eh = ehs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Fault, eh.Flags); Assert.Equal(1, eh.TryOffset); Assert.Equal(15, eh.TryLength); Assert.Equal(16, eh.HandlerOffset); Assert.Equal(14, eh.HandlerLength); Assert.Throws <InvalidOperationException>(() => eh.FilterOffset); Assert.Throws <InvalidOperationException>(() => eh.CatchType); } { MethodInfo m = gt.GetMethod("Filter"); MethodBody body = m.GetMethodBody(); IList <ExceptionHandlingClause> ehs = body.ExceptionHandlingClauses; Assert.Equal(1, ehs.Count); ExceptionHandlingClause eh = ehs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Filter, eh.Flags); Assert.Equal(1, eh.TryOffset); Assert.Equal(15, eh.TryLength); Assert.Equal(40, eh.HandlerOffset); Assert.Equal(16, eh.HandlerLength); Assert.Equal(16, eh.FilterOffset); Assert.Throws <InvalidOperationException>(() => eh.CatchType); } } }
public static void TestMethodBody1() { using (TypeLoader tl = new TypeLoader("mscorlib")) { Assembly coreAssembly = tl.LoadFromStream(TestUtils.CreateStreamForCoreAssembly()); tl.Resolving += delegate(TypeLoader sender, AssemblyName name) { if (name.Name == "mscorlib") { return(coreAssembly); } return(null); }; Assembly a = tl.LoadFromByteArray(TestData.s_AssemblyWithMethodBodyImage); //a = Assembly.Load(TestData.s_AssemblyWithMethodBodyImage); //coreAssembly = typeof(object).Assembly; Type nonsense = a.GetType("Nonsense`1", throwOnError: true); Type theT = nonsense.GetTypeInfo().GenericTypeParameters[0]; MethodInfo m = nonsense.GetMethod("Foo"); Type theM = m.GetGenericArguments()[0]; MethodBody mb = m.GetMethodBody(); byte[] il = mb.GetILAsByteArray(); Assert.Equal <byte>(TestData.s_AssemblyWithMethodBodyILBytes, il); Assert.Equal(4, mb.MaxStackSize); Assert.True(mb.InitLocals); Assert.Equal(0x11000001, mb.LocalSignatureMetadataToken); IList <LocalVariableInfo> lvis = mb.LocalVariables; Assert.Equal(10, lvis.Count); Assert.Equal(0, lvis[0].LocalIndex); Assert.False(lvis[0].IsPinned); Assert.Equal(coreAssembly.GetType("System.Single", throwOnError: true), lvis[0].LocalType); Assert.Equal(1, lvis[1].LocalIndex); Assert.False(lvis[1].IsPinned); Assert.Equal(coreAssembly.GetType("System.Double", throwOnError: true), lvis[1].LocalType); Assert.Equal(2, lvis[2].LocalIndex); Assert.False(lvis[2].IsPinned); Assert.Equal(theT, lvis[2].LocalType); Assert.Equal(3, lvis[3].LocalIndex); Assert.False(lvis[3].IsPinned); Assert.Equal(theT.MakeArrayType(), lvis[3].LocalType); Assert.Equal(4, lvis[4].LocalIndex); Assert.False(lvis[4].IsPinned); Assert.Equal(coreAssembly.GetType("System.Collections.Generic.IList`1", throwOnError: true).MakeGenericType(theM), lvis[4].LocalType); Assert.Equal(5, lvis[5].LocalIndex); Assert.False(lvis[5].IsPinned); Assert.Equal(coreAssembly.GetType("System.String", throwOnError: true), lvis[5].LocalType); Assert.Equal(6, lvis[6].LocalIndex); Assert.False(lvis[6].IsPinned); Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeArrayType(), lvis[6].LocalType); Assert.Equal(7, lvis[7].LocalIndex); Assert.True(lvis[7].IsPinned); Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeByRefType(), lvis[7].LocalType); Assert.Equal(8, lvis[8].LocalIndex); Assert.False(lvis[8].IsPinned); Assert.Equal(coreAssembly.GetType("System.Int32", throwOnError: true).MakeArrayType(), lvis[8].LocalType); Assert.Equal(9, lvis[9].LocalIndex); Assert.False(lvis[9].IsPinned); Assert.Equal(coreAssembly.GetType("System.Boolean", throwOnError: true), lvis[9].LocalType); IList <ExceptionHandlingClause> ehcs = mb.ExceptionHandlingClauses; Assert.Equal(2, ehcs.Count); ExceptionHandlingClause ehc = ehcs[0]; Assert.Equal(ExceptionHandlingClauseOptions.Finally, ehc.Flags); Assert.Equal(97, ehc.TryOffset); Assert.Equal(41, ehc.TryLength); Assert.Equal(138, ehc.HandlerOffset); Assert.Equal(5, ehc.HandlerLength); ehc = ehcs[1]; Assert.Equal(ExceptionHandlingClauseOptions.Filter, ehc.Flags); Assert.Equal(88, ehc.TryOffset); Assert.Equal(58, ehc.TryLength); Assert.Equal(172, ehc.HandlerOffset); Assert.Equal(16, ehc.HandlerLength); Assert.Equal(146, ehc.FilterOffset); } }