public void PinnedAndUnpinnedLocals() { using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(typeof(PinnedAndUnpinnedLocalsToDecode).GetTypeInfo().Assembly))) using (var peReader = new PEReader(stream)) { MetadataReader reader = peReader.GetMetadataReader(); var provider = new DisassemblingTypeProvider(); TypeDefinitionHandle typeDefHandle = TestMetadataResolver.FindTestType(reader, typeof(PinnedAndUnpinnedLocalsToDecode)); TypeDefinition typeDef = reader.GetTypeDefinition(typeDefHandle); MethodDefinition methodDef = reader.GetMethodDefinition(typeDef.GetMethods().First()); Assert.Equal("DoSomething", reader.GetString(methodDef.Name)); MethodBodyBlock body = peReader.GetMethodBody(methodDef.RelativeVirtualAddress); StandaloneSignature localSignature = reader.GetStandaloneSignature(body.LocalSignature); ImmutableArray <string> localTypes = localSignature.DecodeLocalSignature(provider, genericContext: null); // Compiler can generate temporaries or re-order so just check the ones we expect are there. // (They could get optimized away too. If that happens in practice, change this test to use hard-coded signatures.) Assert.Contains("uint8[] pinned", localTypes); Assert.Contains("uint8[]", localTypes); } }
public void AssemblyReflectionOnlyLoadFromBytes() { Assembly assembly = typeof(AssemblyTests).Assembly; byte[] aBytes = System.IO.File.ReadAllBytes(AssemblyPathHelper.GetAssemblyLocation(assembly)); Assert.Throws <PlatformNotSupportedException>(() => Assembly.ReflectionOnlyLoad(aBytes)); }
public void StackTraceSymbolsDoNotLockFile() { var asmPath = AssemblyPathHelper.GetAssemblyLocation(typeof(StackTraceSymbolsTests).Assembly); var pdbPath = Path.ChangeExtension(asmPath, ".pdb"); Assert.True(File.Exists(pdbPath)); new StackTrace(true).GetFrames(); File.Move(pdbPath, pdbPath); }
public void GetFiles() { Assert.NotNull(typeof(AssemblyTests).Assembly.GetFiles()); Assert.Equal(1, typeof(AssemblyTests).Assembly.GetFiles().Length); string name = AssemblyPathHelper.GetAssemblyLocation(typeof(AssemblyTests).Assembly); Assert.Equal(typeof(AssemblyTests).Assembly.GetFiles()[0].Name, name); }
public void LoadFrom_SameIdentityAsAssemblyWithDifferentPath_ReturnsEqualAssemblies() { Assembly assembly1 = Assembly.LoadFrom(AssemblyPathHelper.GetAssemblyLocation(typeof(AssemblyTests).Assembly)); Assert.Equal(assembly1, typeof(AssemblyTests).Assembly); Assembly assembly2 = Assembly.LoadFrom(LoadFromTestPath); Assert.Equal(assembly1, assembly2); }
protected override Assembly Load(AssemblyName assemblyName) { // Override the assembly that was loaded in DefaultContext. string dirName = Path.GetDirectoryName(AssemblyPathHelper.GetAssemblyLocation(typeof(string).Assembly)); string assemblyPath = Path.Combine(dirName, assemblyName.Name + ".dll"); Assembly assembly = LoadFromAssemblyPath(assemblyPath); LoadedFromContext = true; return(assembly); }
public static void Scenario_GetAssemblyName() { // Ensure you can do all this without resolving dependencies. using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(GenericClass1 <>).Assembly)); AssemblyName assemblyName = a.GetName(); Console.WriteLine(assemblyName.FullName); } }
public void CompiledAssembly_GetWithPathToAssemblySet_ReturnsExpectedAssembly() { var results = new CompilerResults(null) { PathToAssembly = AssemblyPathHelper.GetAssemblyLocation(typeof(CompilerResultsTests).Assembly) }; Assert.NotNull(results.CompiledAssembly); Assert.Equal(typeof(CompilerResultsTests).Assembly.FullName, results.CompiledAssembly.FullName); Assert.Same(results.CompiledAssembly, results.CompiledAssembly); }
public void AssemblyLoadFromBytes() { Assembly assembly = typeof(AssemblyTests).Assembly; byte[] aBytes = System.IO.File.ReadAllBytes(AssemblyPathHelper.GetAssemblyLocation(assembly)); Assembly loadedAssembly = Assembly.Load(aBytes); Assert.NotNull(loadedAssembly); Assert.Equal(assembly.FullName, loadedAssembly.FullName); }
public static void CoreGetTypeCacheCoverage3() { using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { // Make sure the tricky corner case of a null/empty namespace is covered. Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(TopLevelType).Assembly)); Type t = a.GetType("TopLevelType", throwOnError: true, ignoreCase: false); Assert.Null(t.Namespace); Assert.Equal("TopLevelType", t.Name); } }
public void GetFile() { Assert.Throws <ArgumentNullException>(() => typeof(AssemblyTests).Assembly.GetFile(null)); AssertExtensions.Throws <ArgumentException>(null, () => typeof(AssemblyTests).Assembly.GetFile("")); Assert.Null(typeof(AssemblyTests).Assembly.GetFile("NonExistentfile.dll")); Assert.NotNull(typeof(AssemblyTests).Assembly.GetFile("System.Reflection.Tests.dll")); string name = AssemblyPathHelper.GetAssemblyLocation(typeof(AssemblyTests).Assembly); Assert.Equal(typeof(AssemblyTests).Assembly.GetFile("System.Reflection.Tests.dll").Name, name); }
public void AssemblyLoadFromBytesWithSymbols() { Assembly assembly = typeof(AssemblyTests).Assembly; byte[] aBytes = System.IO.File.ReadAllBytes(AssemblyPathHelper.GetAssemblyLocation(assembly)); byte[] symbols = System.IO.File.ReadAllBytes((System.IO.Path.ChangeExtension(AssemblyPathHelper.GetAssemblyLocation(assembly), ".pdb"))); Assembly loadedAssembly = Assembly.Load(aBytes, symbols); Assert.NotNull(loadedAssembly); Assert.Equal(assembly.FullName, loadedAssembly.FullName); }
public static void Scenario_EnumerateDependencies() { // Ensure you can do all this without resolving dependencies. using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(GenericClass1 <>).Assembly)); foreach (AssemblyName name in a.GetReferencedAssemblies()) { Console.WriteLine(name.FullName); } } }
public void FullyQualifiedName() { var loc = AssemblyPathHelper.GetAssemblyLocation(Assembly.GetExecutingAssembly()); // Browser will include the path (/), so strip it if (PlatformDetection.IsBrowser && loc.Length > 1) { loc = loc.Substring(1); } Assert.Equal(loc, Module.FullyQualifiedName); }
public void TestCustomAttributeDecoderGenericArray() { Type type = typeof(HasGenericArrayAttributes); using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(type.GetTypeInfo().Assembly))) using (PEReader peReader = new PEReader(stream)) { MetadataReader reader = peReader.GetMetadataReader(); CustomAttributeTypeProvider provider = new CustomAttributeTypeProvider(); TypeDefinitionHandle typeDefHandle = TestMetadataResolver.FindTestType(reader, type); IList <CustomAttributeData> attributes = type.GetCustomAttributesData(); foreach (CustomAttributeHandle attributeHandle in reader.GetCustomAttributes(typeDefHandle)) { CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle); CustomAttributeValue <string> value = attribute.DecodeValue(provider); if (value.FixedArguments.Length == 2) { Assert.Equal(2, value.FixedArguments.Length); ImmutableArray <CustomAttributeTypedArgument <string> > array1 = (ImmutableArray <CustomAttributeTypedArgument <string> >)(value.FixedArguments[0].Value); Assert.Equal("int32[]", value.FixedArguments[0].Type); Assert.Equal(1, array1[0].Value); Assert.Equal(3, array1[2].Value); ImmutableArray <CustomAttributeTypedArgument <string> > array2 = (ImmutableArray <CustomAttributeTypedArgument <string> >)(value.FixedArguments[1].Value); Assert.Equal("uint8[]", value.FixedArguments[1].Type); Assert.Equal((byte)4, array2[0].Value); Assert.Equal((byte)5, array2[1].Value); Assert.Empty(value.NamedArguments); } else { Assert.Equal(1, value.FixedArguments.Length); Assert.Equal("uint8", value.FixedArguments[0].Type); Assert.Equal((byte)1, value.FixedArguments[0].Value); Assert.Equal(2, value.NamedArguments.Length); Assert.Equal("uint8", value.NamedArguments[0].Type); Assert.Equal((byte)2, value.NamedArguments[0].Value); ImmutableArray <CustomAttributeTypedArgument <string> > array = (ImmutableArray <CustomAttributeTypedArgument <string> >)(value.NamedArguments[1].Value); Assert.Equal("uint8[]", value.NamedArguments[1].Type); Assert.Equal((byte)3, array[0].Value); } } } }
public static void TestRestrictions() { using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(TopLevelType).Assembly)); #pragma warning disable SYSLIB0012 Assert.Throws <NotSupportedException>(() => a.CodeBase); Assert.Throws <NotSupportedException>(() => a.EscapedCodeBase); #pragma warning restore SYSLIB0012 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 (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(SampleMetadata.NS0.SameNamedType).Assembly)); // 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 void FullyQualifiedName() { #if SINGLE_FILE_TEST_RUNNER Assert.Equal("<Unknown>", Module.FullyQualifiedName); #else var loc = AssemblyPathHelper.GetAssemblyLocation(Assembly.GetExecutingAssembly()); // Browser will include the path (/), so strip it if (PlatformDetection.IsBrowser && loc.Length > 1) { loc = loc.Substring(1); } Assert.Equal(loc, Module.FullyQualifiedName); #endif }
public void GetFiles() { var asm = typeof(AssemblyTests).Assembly; if (asm.Location.Length > 0) { Assert.NotNull(asm.GetFiles()); Assert.Equal(1, asm.GetFiles().Length); string name = AssemblyPathHelper.GetAssemblyLocation(asm); Assert.Equal(asm.GetFiles()[0].Name, name); } else { Assert.Throws <FileNotFoundException>(() => asm.GetFiles()); } }
public void TestCustomAttributeDecoderGenericUsingReflection() { Type type = typeof(HasGenericAttributes); using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(type.GetTypeInfo().Assembly))) using (PEReader peReader = new PEReader(stream)) { MetadataReader reader = peReader.GetMetadataReader(); CustomAttributeTypeProvider provider = new CustomAttributeTypeProvider(); TypeDefinitionHandle typeDefHandle = TestMetadataResolver.FindTestType(reader, type); IList <CustomAttributeData> attributes = type.GetCustomAttributesData(); int i = 0; foreach (CustomAttributeHandle attributeHandle in reader.GetCustomAttributes(typeDefHandle)) { CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle); CustomAttributeValue <string> value = attribute.DecodeValue(provider); CustomAttributeData reflectionAttribute = attributes[i++]; Assert.Equal(reflectionAttribute.ConstructorArguments.Count, value.FixedArguments.Length); Assert.Equal(reflectionAttribute.NamedArguments.Count, value.NamedArguments.Length); int j = 0; foreach (CustomAttributeTypedArgument <string> arguments in value.FixedArguments) { Assert.Equal(TypeToString(reflectionAttribute.ConstructorArguments[j].ArgumentType), arguments.Type); if (reflectionAttribute.ConstructorArguments[j].Value.ToString() != arguments.Value.ToString()) { Assert.Equal(reflectionAttribute.ConstructorArguments[j].Value, arguments.Value); } j++; } j = 0; foreach (CustomAttributeNamedArgument <string> arguments in value.NamedArguments) { Assert.Equal(TypeToString(reflectionAttribute.NamedArguments[j].TypedValue.ArgumentType), arguments.Type); if (reflectionAttribute.NamedArguments[j].TypedValue.Value.ToString() != arguments.Value.ToString()) { Assert.Equal(reflectionAttribute.NamedArguments[j].TypedValue.Value, arguments.Value); } j++; } } } }
public static void PathAssemblyResolverBasicPathWithRunningAssemblies() { string coreAssemblyPath = TestUtils.GetPathToCoreAssembly(); // Obtain this test class string thisAssemblyPath = AssemblyPathHelper.GetAssemblyLocation(typeof(MetadataLoadContextTests).Assembly); var resolver = new PathAssemblyResolver(new string[] { coreAssemblyPath, thisAssemblyPath }); using (MetadataLoadContext lc = new MetadataLoadContext(resolver, TestUtils.GetNameOfCoreAssembly())) { AssemblyName thisAssemblyName = typeof(MetadataLoadContextTests).Assembly.GetName(); Assembly assembly = lc.LoadFromAssemblyName(thisAssemblyName); Type t = assembly.GetType(typeof(MetadataLoadContextTests).FullName, throwOnError: true); Assert.Equal(t.FullName, typeof(MetadataLoadContextTests).FullName); Assert.Equal(t.Assembly.Location, thisAssemblyPath); } }
public void WrongSignatureType() { using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(typeof(VarArgsToDecode).GetTypeInfo().Assembly))) using (var peReader = new PEReader(stream)) { MetadataReader reader = peReader.GetMetadataReader(); var provider = new DisassemblingTypeProvider(); var decoder = new SignatureDecoder <string, DisassemblingGenericContext>(provider, reader, genericContext: null); BlobReader fieldSignature = reader.GetBlobReader(reader.GetFieldDefinition(MetadataTokens.FieldDefinitionHandle(1)).Signature); BlobReader methodSignature = reader.GetBlobReader(reader.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(1)).Signature); BlobReader propertySignature = reader.GetBlobReader(reader.GetPropertyDefinition(MetadataTokens.PropertyDefinitionHandle(1)).Signature); Assert.Throws <BadImageFormatException>(() => decoder.DecodeMethodSignature(ref fieldSignature)); Assert.Throws <BadImageFormatException>(() => decoder.DecodeFieldSignature(ref methodSignature)); Assert.Throws <BadImageFormatException>(() => decoder.DecodeLocalSignature(ref propertySignature)); } }
public void AssemblyLoadFromBytes() { Assembly assembly = typeof(AssemblyTests).Assembly; byte[] aBytes = System.IO.File.ReadAllBytes(AssemblyPathHelper.GetAssemblyLocation(assembly)); Assembly loadedAssembly = Assembly.Load(aBytes); Assert.NotNull(loadedAssembly); Assert.Equal(assembly.FullName, loadedAssembly.FullName); System.Runtime.Loader.AssemblyLoadContext alc = System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(loadedAssembly); string expectedName = "Assembly.Load(byte[], ...)"; Assert.Equal(expectedName, alc.Name); Assert.Contains(expectedName, alc.ToString()); Assert.Contains("System.Runtime.Loader.IndividualAssemblyLoadContext", alc.ToString()); }
public void GetFile() { var asm = typeof(AssemblyTests).Assembly; if (asm.Location.Length > 0) { Assert.Throws <ArgumentNullException>(() => asm.GetFile(null)); Assert.Throws <ArgumentException>(() => asm.GetFile("")); Assert.Null(asm.GetFile("NonExistentfile.dll")); Assert.NotNull(asm.GetFile("System.Reflection.Tests.dll")); string name = AssemblyPathHelper.GetAssemblyLocation(asm); Assert.Equal(asm.GetFile("System.Reflection.Tests.dll").Name, name); } else { Assert.Throws <FileNotFoundException>(() => asm.GetFile("System.Reflection.Tests.dll")); } }
// Given a runtime Type, load up the equivalent in the Test MetataLoadContext. This is for test-writing convenience so // that tests can write "typeof(TestClass).Project()" and get the benefits of compile-time typename checking and Intellisense. // It also opens the possibility of sharing Reflection tests between different type providers with minimal fuss. public static Type Project(this Type type) { if (type == null) { return(null); } Assembly assembly = type.Assembly; string location = assembly.Location; if (PlatformDetection.IsNotBrowser && (location == null || location == string.Empty)) { throw new Exception("Could not find the IL for assembly " + type.Assembly + " on disk. The most likely cause " + "is that you built the tests for a Jitted runtime but are running them on an AoT runtime."); } Assembly projectedAssembly = s_assemblyDict.GetOrAdd(assembly, delegate(Assembly a) { // The core assembly we're using might not be the one powering the runtime. Make sure we project to the core assembly the MetataLoadContext // is using. if (a == typeof(object).Assembly) { TestMetadataLoadContext.LoadFromStream(CreateStreamForCoreAssembly()); } return(TestMetadataLoadContext.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(a))); }); Type projectedType = s_typeDict.GetOrAdd(type, (t) => projectedAssembly.GetType(t.FullName, throwOnError: true, ignoreCase: false)); if (s_useRuntimeTypesForTests.Value) { return(type); } return(projectedType); }
public void DecodeVarArgsDefAndRef() { using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(typeof(VarArgsToDecode).GetTypeInfo().Assembly))) using (var peReader = new PEReader(stream)) { MetadataReader metadataReader = peReader.GetMetadataReader(); TypeDefinitionHandle typeDefHandle = TestMetadataResolver.FindTestType(metadataReader, typeof(VarArgsToDecode)); TypeDefinition typeDef = metadataReader.GetTypeDefinition(typeDefHandle); MethodDefinition methodDef = metadataReader.GetMethodDefinition(typeDef.GetMethods().First()); Assert.Equal("VarArgsCallee", metadataReader.GetString(methodDef.Name)); var provider = new OpaqueTokenTypeProvider(); MethodSignature <string> defSignature = methodDef.DecodeSignature(provider, null); Assert.Equal(SignatureCallingConvention.VarArgs, defSignature.Header.CallingConvention); Assert.Equal(1, defSignature.RequiredParameterCount); Assert.Equal(new[] { "int32" }, defSignature.ParameterTypes); int refCount = 0; foreach (MemberReferenceHandle memberRefHandle in metadataReader.MemberReferences) { MemberReference memberRef = metadataReader.GetMemberReference(memberRefHandle); if (metadataReader.StringComparer.Equals(memberRef.Name, "VarArgsCallee")) { Assert.Equal(MemberReferenceKind.Method, memberRef.GetKind()); MethodSignature <string> refSignature = memberRef.DecodeMethodSignature(provider, null); Assert.Equal(SignatureCallingConvention.VarArgs, refSignature.Header.CallingConvention); Assert.Equal(1, refSignature.RequiredParameterCount); Assert.Equal(new[] { "int32", "bool", "string", "float64" }, refSignature.ParameterTypes); refCount++; } } Assert.Equal(1, refCount); } }
public SatelliteAssembliesTestsFixture() { AssemblyLoadContext satelliteAssembliesTests = new AssemblyLoadContext("SatelliteAssembliesTests"); var satelliteAssembliesTestsPath = AssemblyPathHelper.GetAssemblyLocation(typeof(SatelliteAssembliesTests).Assembly); satelliteAssembliesTests.LoadFromAssemblyPath(satelliteAssembliesTestsPath); AssemblyLoadContext referencedClassLib = new AssemblyLoadContext("ReferencedClassLib"); var referencedClassLibPath = AssemblyPathHelper.GetAssemblyLocation(typeof(ReferencedClassLib.Program).Assembly); referencedClassLib.LoadFromAssemblyPath(referencedClassLibPath); AssemblyLoadContext referencedClassLibNeutralIsSatellite = new AssemblyLoadContext("ReferencedClassLibNeutralIsSatellite"); var referencedClassLibNeutralIsSatellitePath = AssemblyPathHelper.GetAssemblyLocation(typeof(ReferencedClassLibNeutralIsSatellite.Program).Assembly); referencedClassLibNeutralIsSatellite.LoadFromAssemblyPath(referencedClassLibNeutralIsSatellitePath); new AssemblyLoadContext("Empty"); try { Assembly assembly = Assembly.LoadFile(satelliteAssembliesTestsPath); contexts["LoadFile"] = AssemblyLoadContext.GetLoadContext(assembly); } catch (Exception e) { Console.WriteLine(e); } foreach (var alc in AssemblyLoadContext.All) { if (alc.Name != null) { contexts[alc.Name] = alc; } } }
public static void Scenario_FindACoreAssembly() { // Ensure you can do all this without setting a core assembly. using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly[] candidates = { lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(GenericClass1 <>).Assembly)), lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(object).Assembly)), }; foreach (Assembly candidate in candidates) { Type objectType = candidate.GetType("System.Object", throwOnError: false); if (objectType != null) { // Found our core assembly. return; } } Assert.True(false, "Did not find a core assembly."); } }
public static void Scenario_EnumerateTypesAndMembers() { // Ensure you can do all this without resolving dependencies. using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver())) { Assembly a = lc.LoadFromAssemblyPath(AssemblyPathHelper.GetAssemblyLocation(typeof(GenericClass1 <>).Assembly)); 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()); } } } }
public static void GetAssemblyName() { AssertExtensions.Throws <ArgumentNullException>("assemblyFile", () => AssemblyName.GetAssemblyName(null)); AssertExtensions.Throws <ArgumentException>("path", null, () => AssemblyName.GetAssemblyName(string.Empty)); Assert.Throws <System.IO.FileNotFoundException>(() => AssemblyName.GetAssemblyName("IDontExist")); using (var tempFile = new TempFile(Path.GetTempFileName(), 0)) // Zero-size file { Assert.Throws <System.BadImageFormatException>(() => AssemblyName.GetAssemblyName(tempFile.Path)); } using (var tempFile = new TempFile(Path.GetTempFileName(), 42)) { Assert.Throws <System.BadImageFormatException>(() => AssemblyName.GetAssemblyName(tempFile.Path)); } Assembly a = typeof(AssemblyNameTests).Assembly; Assert.Equal(new AssemblyName(a.FullName).ToString(), AssemblyName.GetAssemblyName(AssemblyPathHelper.GetAssemblyLocation(a)).ToString()); }