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 AssemblyName_GetName_SimpleNameOnly() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromByteArray(TestData.s_SimpleNameOnlyImage); AssemblyName an = a.GetName(copiedName: false); Assert.Equal("SimpleNameOnly", an.Name); Assert.Equal(AssemblyNameFlags.PublicKey, an.Flags); Version v = an.Version; Assert.NotNull(v); Assert.Equal(0, v.Major); Assert.Equal(0, v.Minor); Assert.Equal(0, v.Build); Assert.Equal(0, v.Revision); string cultureName = an.CultureName; Assert.Equal(string.Empty, cultureName); byte[] publicKey = an.GetPublicKey(); Assert.Equal(0, publicKey.Length); Assert.Equal(AssemblyContentType.Default, an.ContentType); Assert.Equal(AssemblyHashAlgorithm.SHA1, an.HashAlgorithm); Assembly aAgain = tl.LoadFromAssemblyName(an); Assert.Equal(a, aAgain); } }
public static void LoadFromAssemblyNameNeverLoadedAssembly() { using (TypeLoader tl = new TypeLoader()) { Assert.Throws <FileNotFoundException>(() => tl.LoadFromAssemblyName("NeverSawThis")); } }
public static void LoadFromAssemblyNameNullAssemblyName() { using (TypeLoader tl = new TypeLoader()) { Assert.Throws <ArgumentNullException>(() => tl.LoadFromAssemblyName((AssemblyName)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 RoType LoadTypeFromAssemblyQualifiedName(string name, RoAssembly defaultAssembly, bool ignoreCase, bool throwOnError) { if (!name.TypeNameContainsTypeParserMetacharacters()) { // Fast-path: the type contains none of the parser metacharacters nor the escape character. Just treat as plain old type name. name.SplitTypeName(out string ns, out string simpleName); RoType type = defaultAssembly.GetTypeCore(ns, simpleName, ignoreCase: ignoreCase, out Exception e); if (type != null) { return(type); } if (throwOnError) { throw e; } } TypeLoader loader = defaultAssembly.Loader; Func <AssemblyName, Assembly> assemblyResolver = delegate(AssemblyName assemblyName) { return(loader.LoadFromAssemblyName(assemblyName)); }; Func <Assembly, string, bool, Type> typeResolver = delegate(Assembly assembly, string fullName, bool ignoreCase2) { if (assembly == null) { assembly = defaultAssembly; } Debug.Assert(assembly is RoAssembly); RoAssembly roAssembly = (RoAssembly)assembly; fullName = fullName.UnescapeTypeNameIdentifier(); fullName.SplitTypeName(out string ns, out string simpleName); Type type = roAssembly.GetTypeCore(ns, simpleName, ignoreCase: ignoreCase2, out Exception e); if (type != null) { return(type); } if (throwOnError) { throw e; } return(null); }; return((RoType)Type.GetType(name, assemblyResolver: assemblyResolver, typeResolver: typeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase)); }
public static void AssemblyName_GetName_HashWithSha256() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromByteArray(TestData.s_HashWithSha256Image); AssemblyName an = a.GetName(copiedName: false); Assert.Equal("HashWithSha256", an.Name); Assert.Equal(AssemblyHashAlgorithm.SHA256, an.HashAlgorithm); Assembly aAgain = tl.LoadFromAssemblyName(an); Assert.Equal(a, aAgain); } }
public static void AssemblyName_GetName_CultureFrCh() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromByteArray(TestData.s_CultureFrChImage); AssemblyName an = a.GetName(copiedName: false); Assert.Equal("CultureFrCh", an.Name); string cultureName = an.CultureName; Assert.Equal("fr-CH", cultureName); Assembly aAgain = tl.LoadFromAssemblyName(an); Assert.Equal(a, aAgain); } }
public static void LoadExternalAssembly1() { using (TypeLoader tl = new TypeLoader()) { Assembly runtimeAssembly = typeof(object).Assembly; // Intentionally not projected. string location = runtimeAssembly.Location; tl.Resolving += delegate(TypeLoader sender, AssemblyName an) { return(runtimeAssembly); }; Assert.Throws <FileLoadException>(() => tl.LoadFromAssemblyName("DontCare")); } }
public static void AssemblyName_GetName_PublicKeyToken1ee753223f71263d() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromByteArray(TestData.s_PublicKeyToken1ee753223f71263dImage); AssemblyName an = a.GetName(copiedName: false); Assert.Equal("PublicKeyToken1ee753223f71263d", an.Name); Assert.Equal(AssemblyNameFlags.PublicKey, an.Flags); byte[] publicKey = an.GetPublicKey(); Assert.Equal <byte>(TestData.s_PublicKeyToken1ee753223f71263d_Pk, publicKey); Assembly aAgain = tl.LoadFromAssemblyName(an); Assert.Equal(a, aAgain); } }
public static void AssemblyName_GetName_Version1_2_65535_65535() { using (TypeLoader tl = new TypeLoader()) { Assembly a = tl.LoadFromByteArray(TestData.s_Version1_2_65535_65535Image); AssemblyName an = a.GetName(copiedName: false); Assert.Equal("Version1_2_65535_65535", an.Name); Version v = an.Version; Assert.NotNull(v); Assert.Equal(1, v.Major); Assert.Equal(2, v.Minor); Assert.Equal(65535, v.Build); Assert.Equal(65535, v.Revision); Assembly aAgain = tl.LoadFromAssemblyName(an); Assert.Equal(a, aAgain); } }
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 CoreAssemblyCanBeAFacade() { using (TypeLoader tl = new TypeLoader(coreAssemblyName: TestData.s_PhonyCoreAssemblyName)) { Assembly actualCoreAssembly = null; tl.Resolving += delegate(TypeLoader sender, AssemblyName refName) { if (refName.Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)) { return(actualCoreAssembly = sender.LoadFromStream(TestUtils.CreateStreamForCoreAssembly())); } return(null); }; Assembly a = tl.LoadFromByteArray(TestData.s_PhonyCoreAssemblyImage); // This is a sanity check to ensure that "TestData.s_PhonyCoreAssemblyName" is actually the def-name of this // assembly. It better be since we told our TypeLoader to use it as our core assembly. Assembly aAgain = tl.LoadFromAssemblyName(TestData.s_PhonyCoreAssemblyName); Type derived = a.GetType("Derived", throwOnError: true, ignoreCase: false); // Calling BaseType causes the TypeLoader to parse the typespec "Base<object>". Since "object" is a primitive // type, it should be encoded using the short-form "ELEMENT_TYPE_OBJECT." Hence, the TypeLoader is forced // to look up "System.Object" in the core assembly we assigned to it, which in this case is "PhonyCoreAssembly" // which type-forwards System.Object to "mscorlib". Type baseType = derived.BaseType; Assert.NotNull(actualCoreAssembly); // Ensure our resolve handler actually ran. Assert.NotEqual(a, actualCoreAssembly); Assert.True(baseType.IsConstructedGenericType); Type retrievedObjectType = baseType.GenericTypeArguments[0]; Assert.Equal("System.Object", retrievedObjectType.FullName); Assert.Equal(actualCoreAssembly, retrievedObjectType.Assembly); } }