public static void Scenario_FindACoreAssembly() { // Ensure you can do all this without setting a core assembly. using (TypeLoader tl = new TypeLoader()) { Assembly[] candidates = { tl.LoadFromAssemblyPath(typeof(GenericClass1 <>).Assembly.Location), tl.LoadFromAssemblyPath(typeof(object).Assembly.Location), }; foreach (Assembly candidate in candidates) { Type objectType = candidate.GetType("System.Object", throwOnError: false); if (objectType != null) { // Found our core assembly. Ensure it's not too late to set the CoreAssemblyName property. tl.CoreAssemblyName = objectType.Assembly.GetName().FullName; return; } } Assert.True(false, "Did not find a core assembly."); } }
public static void ModuleGetTypes() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Type[] types = a.ManifestModule.GetTypes(); Assert.Equal(2, types.Length); AssertMainModuleTypesFound(types, a); Module bob = a.GetModule("Bob.netmodule"); Assert.NotNull(bob); Type[] bobTypes = bob.GetTypes(); Assert.Equal(2, bobTypes.Length); AssertBobModuleTypesFound(bobTypes, a); } } }
public static void LoadFromAssemblyPathNull() { using (TypeLoader tl = new TypeLoader()) { Assert.Throws <ArgumentNullException>(() => tl.LoadFromAssemblyPath(null)); } }
// // For a given assembly name and its full path, Reflection-Only load the assembly directly // from the file in disk or load the file to memory and then create assembly instance from // memory buffer data. // private static Assembly ReflectionOnlyLoadAssembly(string assemblyName, string fullPathToAssembly) { Assembly assembly = null; // If the assembly path is empty, try to load assembly by name. LoadFromAssemblyName // will result in a TypeLoader.Resolve event that will contain more information about the // requested assembly. if (String.IsNullOrEmpty(fullPathToAssembly)) { return(_typeLoader.LoadFromAssemblyName(assemblyName)); } else if (_cachedTypeLoaderAssemblies.TryGetValue(fullPathToAssembly, out assembly)) { return(assembly); } else if (!String.IsNullOrEmpty(assemblyName) && _cachedTypeLoaderAssemblies.TryGetValue(assemblyName, out assembly)) { return(assembly); } else { assembly = _typeLoader.LoadFromAssemblyPath(fullPathToAssembly); } // Add the assembly to the cache. ReflectionHelper.ReflectionOnlyLoadAssembly // receives frequent calls requesting the same assembly. if (assembly != null && fullPathToAssembly != null) { _cachedTypeLoaderAssemblies.Add(fullPathToAssembly, assembly); _cachedTypeLoaderReferencePaths.Add(Path.GetDirectoryName(fullPathToAssembly)); } return(assembly); }
public static void GetLoadModules2() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "n.dll"); string myRes1Path = Path.Combine(td.Path, "MyRes1"); string myRes2Path = Path.Combine(td.Path, "MyRes2"); File.WriteAllBytes(assemblyPath, TestData.s_AssemblyWithResourcesInManifestFilesImage); File.WriteAllBytes(myRes1Path, TestData.s_MyRes1); File.WriteAllBytes(myRes2Path, TestData.s_MyRes2); using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module res1 = a.GetModule("MyRes1"); Module res2 = a.GetModule("MyRes2"); { Module[] modules = a.GetLoadedModules(getResourceModules: true); Assert.Equal(3, modules.Length); Assert.Contains <Module>(a.ManifestModule, modules); Assert.Contains <Module>(res1, modules); Assert.Contains <Module>(res2, modules); } { Module[] modules = a.GetLoadedModules(getResourceModules: false); Assert.Equal(1, modules.Length); Assert.Equal(a.ManifestModule, modules[0]); } } } }
public static void AssemblyLocationFile() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(tf.Path); string location = a.Location; Assert.Equal(tf.Path, location); } }
public static void Scenario_GetAssemblyName() { // Ensure you can do all this without resolving dependencies. using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(typeof(GenericClass1 <>).Assembly.Location); AssemblyName assemblyName = a.GetName(); Console.WriteLine(assemblyName.FullName); } }
public static void CoreGetTypeCacheCoverage3() { using (TypeLoader tl = new TypeLoader()) { // Make sure the tricky corner case of a null/empty namespace is covered. Assembly a = tl.LoadFromAssemblyPath(typeof(TopLevelType).Assembly.Location); Type t = a.GetType("TopLevelType", throwOnError: true, ignoreCase: false); Assert.Equal(null, t.Namespace); Assert.Equal("TopLevelType", t.Name); } }
public static void ModuleFullyQualifiedNameFromPath() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) using (TypeLoader tl = new TypeLoader()) { string path = tf.Path; Assembly a = tl.LoadFromAssemblyPath(path); Module m = a.ManifestModule; Assert.Equal(path, m.FullyQualifiedName); } }
public static void ModuleGetNameFromPath() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) using (TypeLoader tl = new TypeLoader()) { string path = tf.Path; Assembly a = tl.LoadFromAssemblyPath(path); Module m = a.ManifestModule; string name = Path.GetFileName(path); Assert.Equal(name, m.Name); } }
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 Scenario_EnumerateDependencies() { // Ensure you can do all this without resolving dependencies. using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(typeof(GenericClass1 <>).Assembly.Location); foreach (AssemblyName name in a.GetReferencedAssemblies()) { Console.WriteLine(name.FullName); } } }
public static void LoadMultiModuleFromDisk_GetModuleNameNotThere() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Assert.Throws <FileNotFoundException>(() => a.GetModule("Bob.netmodule")); } } }
public static void LoadFromDifferentLocations() { using (TempFile tf1 = TempFile.Create(TestData.s_SimpleAssemblyImage)) using (TempFile tf2 = TempFile.Create(TestData.s_SimpleAssemblyImage)) using (TypeLoader tl = new TypeLoader()) { // As long as the MVID matches, you can load the same assembly from multiple locations. Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage); Assert.NotNull(a); Assembly a1 = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage); Assert.Equal(a, a1); Assembly a2 = tl.LoadFromAssemblyName(new AssemblyName(TestData.s_SimpleAssemblyName)); Assert.Equal(a, a2); Assembly a3 = tl.LoadFromAssemblyPath(tf1.Path); Assert.Equal(a, a3); Assembly a4 = tl.LoadFromAssemblyPath(tf2.Path); Assert.Equal(a, a4); } }
public static void AssemblyWithResourcesInManifestFile() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "n.dll"); string myRes1Path = Path.Combine(td.Path, "MyRes1"); string myRes2Path = Path.Combine(td.Path, "MyRes2"); string myRes3Path = Path.Combine(td.Path, "MyRes3"); File.WriteAllBytes(assemblyPath, TestData.s_AssemblyWithResourcesInManifestFilesImage); File.WriteAllBytes(myRes1Path, TestData.s_MyRes1); File.WriteAllBytes(myRes2Path, TestData.s_MyRes2); File.WriteAllBytes(myRes3Path, TestData.s_MyRes3); using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); string[] names = a.GetManifestResourceNames().OrderBy(s => s).ToArray(); Assert.Equal <string>(new string[] { "MyRes1", "MyRes2", "MyRes3" }, names); foreach (string name in names) { ManifestResourceInfo mri = a.GetManifestResourceInfo(name); Assert.Equal(default(ResourceLocation), mri.ResourceLocation); Assert.Equal(name, mri.FileName); Assert.Null(mri.ReferencedAssembly); } using (Stream s = a.GetManifestResourceStream("MyRes1")) { byte[] res = s.ToArray(); Assert.Equal <byte>(TestData.s_MyRes1, res); } using (Stream s = a.GetManifestResourceStream("MyRes2")) { byte[] res = s.ToArray(); Assert.Equal <byte>(TestData.s_MyRes2, res); } using (Stream s = a.GetManifestResourceStream("MyRes3")) { byte[] res = s.ToArray(); Assert.Equal <byte>(TestData.s_MyRes3, res); } } } }
public static void TestRestrictions() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(typeof(TopLevelType).Assembly.Location); Assert.Throws <NotSupportedException>(() => a.CodeBase); Assert.Throws <NotSupportedException>(() => a.EscapedCodeBase); Assert.Throws <NotSupportedException>(() => a.GetObjectData(null, default)); Assert.Throws <NotSupportedException>(() => a.GetSatelliteAssembly(null)); Assert.Throws <NotSupportedException>(() => a.GetSatelliteAssembly(null, null)); foreach (TypeInfo t in a.DefinedTypes) { Assert.Throws <InvalidOperationException>(() => t.IsSecurityCritical); Assert.Throws <InvalidOperationException>(() => t.IsSecuritySafeCritical); Assert.Throws <InvalidOperationException>(() => t.IsSecurityTransparent); foreach (MemberInfo mem in t.GetMember("*", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)) { ICustomAttributeProvider icp = mem; Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(inherit: false)); Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(null, inherit: false)); Assert.Throws <InvalidOperationException>(() => icp.IsDefined(null, inherit: false)); if (mem is ConstructorInfo c) { Assert.Throws <InvalidOperationException>(() => c.Invoke(Array.Empty <object>())); Assert.Throws <InvalidOperationException>(() => c.Invoke(default(BindingFlags), null, Array.Empty <object>(), null)); Assert.Throws <InvalidOperationException>(() => c.Invoke(null, Array.Empty <object>())); Assert.Throws <InvalidOperationException>(() => c.Invoke(null, default(BindingFlags), null, Array.Empty <object>(), null)); Assert.Throws <InvalidOperationException>(() => c.MethodHandle); Assert.Throws <InvalidOperationException>(() => c.IsSecurityCritical); Assert.Throws <InvalidOperationException>(() => c.IsSecuritySafeCritical); Assert.Throws <InvalidOperationException>(() => c.IsSecurityTransparent); } if (mem is EventInfo e) { Assert.Throws <InvalidOperationException>(() => e.AddEventHandler(null, null)); Assert.Throws <InvalidOperationException>(() => e.RemoveEventHandler(null, null)); } if (mem is FieldInfo f) { Assert.Throws <InvalidOperationException>(() => f.FieldHandle); Assert.Throws <InvalidOperationException>(() => f.GetValue(null)); Assert.Throws <InvalidOperationException>(() => f.GetValueDirect(default));
public static void CoreGetTypeCacheCoverage2() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(typeof(SampleMetadata.NS0.SameNamedType).Assembly.Location); // Create big hash collisions in GetTypeCoreCache. for (int i = 0; i < 16; i++) { string ns = "SampleMetadata.NS" + i; string name = "SameNamedType"; string fullName = ns + "." + name; Type t = a.GetType(fullName, throwOnError: true); Assert.Equal(fullName, t.FullName); } } }
public static void LoadMultiModuleFromDisk_GetModuleNull() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Assert.Throws <ArgumentNullException>(() => a.GetModule(null)); } } }
public static void ModuleGetTypesReturnsNewObjectEachType() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); TestUtils.AssertNewObjectReturnedEachTime(() => a.ManifestModule.GetTypes()); } } }
public static void ResourceOnlyModules() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "n.dll"); string myRes1Path = Path.Combine(td.Path, "MyRes1"); string myRes2Path = Path.Combine(td.Path, "MyRes2"); string myRes3Path = Path.Combine(td.Path, "MyRes3"); File.WriteAllBytes(assemblyPath, TestData.s_AssemblyWithResourcesInManifestFilesImage); File.WriteAllBytes(myRes1Path, TestData.s_MyRes1); File.WriteAllBytes(myRes2Path, TestData.s_MyRes2); File.WriteAllBytes(myRes3Path, TestData.s_MyRes3); using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module[] modules1 = a.GetModules(getResourceModules: false); Assert.Equal <Module>(new Module[] { a.ManifestModule }, modules1); Module[] modules2 = a.GetModules(getResourceModules: true); Assert.Equal(4, modules2.Length); Module m = a.GetModule("MyRes2"); Assert.NotNull(m); Assert.True(m.IsResource()); Assert.Throws <InvalidOperationException>(() => m.ModuleVersionId); Assert.Equal(0, m.MetadataToken); Assert.Equal("MyRes2", m.ScopeName); Assert.Equal("MyRes2", m.Name); Assert.Equal(myRes2Path, m.FullyQualifiedName); m.GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine); Assert.Equal(PortableExecutableKinds.NotAPortableExecutableImage, peKind); Assert.Equal(default(ImageFileMachine), machine); Assert.True(!m.GetCustomAttributesData().Any()); const BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; Assert.Null(m.GetField("ANY", bf)); Assert.Null(m.GetMethod("ANY")); Assert.True(!m.GetFields(bf).Any()); Assert.True(!m.GetMethods(bf).Any()); Assert.True(!m.GetTypes().Any()); } } }
public static void LoadMultiModuleFromDisk_GetModuleManifest() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module m = a.GetModule("Main.dll"); Assert.Equal(a.ManifestModule, m); } } }
public static void MultiModule_AssemblyDefinedTypes() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Type[] types = a.DefinedTypes.ToArray(); AssertContentsOfMultiModule(types, a); } } }
public static void DisposingReleasesFileLocks() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) { using (TypeLoader tl = new TypeLoader()) { tl.LoadFromAssemblyPath(tf.Path); } try { File.OpenWrite(tf.Path).Close(); } catch (Exception) { Assert.True(false, "PE image file still locked after disposing TypeLoader: " + tf.Path); } } }
public static void LoadFromFileSimpleAssembly() { using (TempFile tf = TempFile.Create(TestData.s_SimpleAssemblyImage)) { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(tf.Path); 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 CrossModuleTypeRefResolution() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module bob = a.GetModule("Bob.netmodule"); Type mainType1 = a.ManifestModule.GetType("MainType1", throwOnError: true, ignoreCase: false); Type baseType = mainType1.BaseType; Assert.Equal("JoeType1", baseType.FullName); Assert.Equal(bob, baseType.Module); } } }
public static void LoadMultiModuleFromDisk_GetModuleCaseInsensitive() { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module m = a.GetModule("bOB.nEtmODule"); Assert.Equal(a, m.Assembly); Assert.Equal(bobNetModulePath, m.FullyQualifiedName); Assert.Equal(Path.GetFileName(bobNetModulePath), m.Name); Assert.Equal(TestData.s_JoeScopeName, m.ScopeName); Assert.Equal(TestData.s_JoeNetModuleMvid, m.ModuleVersionId); } } }
public static void MultiModule_GetModules(bool getResourceModules) { using (TempDirectory td = new TempDirectory()) { string assemblyPath = Path.Combine(td.Path, "MultiModule.dll"); string bobNetModulePath = Path.Combine(td.Path, "Bob.netmodule"); File.WriteAllBytes(assemblyPath, TestData.s_MultiModuleDllImage); File.WriteAllBytes(bobNetModulePath, TestData.s_JoeNetModuleImage); // Note: ScopeName ("Joe") intentionally different from manifest name ("Bob") using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(assemblyPath); Module[] ms = a.GetModules(getResourceModules: getResourceModules); Assert.Equal(2, ms.Length); Module bob = a.GetModule("Bob.netmodule"); Assert.NotNull(bob); Assert.Contains <Module>(a.ManifestModule, ms); Assert.Contains <Module>(bob, ms); } } }
public static void Scenario_EnumerateTypesAndMembers() { // Ensure you can do all this without resolving dependencies. using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromAssemblyPath(typeof(GenericClass1 <>).Assembly.Location); foreach (TypeInfo t in a.DefinedTypes) { Console.WriteLine(t.FullName); foreach (ConstructorInfo c in t.DeclaredConstructors) { Console.WriteLine(" " + c.ToString()); } foreach (MethodInfo m in t.DeclaredMethods) { Console.WriteLine(" " + m.ToString()); } foreach (PropertyInfo p in t.DeclaredProperties) { Console.WriteLine(" " + p.ToString()); } foreach (FieldInfo f in t.DeclaredFields) { Console.WriteLine(" " + f.ToString()); } foreach (EventInfo e in t.DeclaredEvents) { Console.WriteLine(" " + e.ToString()); } } } }