示例#1
0
        public static void AssemblyGetForwardedTypes2()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly upper = lc.LoadFromByteArray(TestData.s_UpperImage);
                Assembly middle = lc.LoadFromByteArray(TestData.s_MiddleImage);

                ReflectionTypeLoadException re = Assert.Throws<ReflectionTypeLoadException>(() => upper.GetForwardedTypesThunk());
                Assert.Equal(3, re.Types.Length);
                Assert.Equal(3, re.LoaderExceptions.Length);

                Assert.Equal(2, re.Types.Count((t) => t == null));
                Assert.Contains<Type>(middle.GetType("Upper2", throwOnError: true), re.Types);

                Assert.Equal(1, re.LoaderExceptions.Count((t) => t == null));
                Assert.True(re.LoaderExceptions.All((t) => t == null || t is FileNotFoundException));
            }
        }
示例#2
0
 public static void ModuleIsResource()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.False(m.IsResource());
     }
 }
示例#3
0
 public static void ModuleFullyQualifiedNameFromByteArray()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.Equal(string.Empty, m.FullyQualifiedName);
     }
 }
示例#4
0
 public static void ModuleScopeNameFromByteArray()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.Equal("SimpleAssembly.dll", m.ScopeName);
     }
 }
示例#5
0
 public static void ModuleMetadataToken()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.Equal(0x00000001, m.MetadataToken);
     }
 }
示例#6
0
 public static void LoadModule_Null()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_MultiModuleDllImage);
         Assert.Throws <ArgumentNullException>(() => a.LoadModule(null, TestData.s_JoeNetModuleImage));
         Assert.Throws <ArgumentNullException>(() => a.LoadModule("Bob.netmodule", null));
     }
 }
示例#7
0
 public static void ResourceDoesNotExist_GetManifestResourceStream_ReturnsNull()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_AssemblyWithEmbeddedResourcesImage);
         Stream?  r = a.GetManifestResourceStream("ResourceThatDoesNotExist");
         Assert.Null(r);
     }
 }
示例#8
0
 public static void AssemblyFullName()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a        = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         string   fullName = a.FullName;
         Assert.Equal(TestData.s_SimpleAssemblyFullName, fullName);
     }
 }
示例#9
0
 public static void AssemblyEntryPoint2()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly   a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         MethodInfo m = a.EntryPoint;
         Assert.Null(m);
     }
 }
示例#10
0
 public static void AssemblyLocationMemory()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a        = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         string   location = a.Location;
         Assert.Equal(string.Empty, location);
     }
 }
示例#11
0
 public static void AssemblyMetadataVersion2_0()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_NetFx20AssemblyImage);
         string   metadataVersion = a.ImageRuntimeVersion;
         Assert.Equal("v2.0.50727", metadataVersion);
     }
 }
示例#12
0
        public static void ExtraDisposesIgnored()
        {
            MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver());

            lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
            lc.Dispose();
            lc.Dispose();
            lc.Dispose();
        }
示例#13
0
 public static void GetLoadModulesReturnsUniqueArrays()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a  = lc.LoadFromByteArray(TestData.s_MultiModuleDllImage);
         Module   m1 = a.LoadModule("Bob.netmodule", TestData.s_JoeNetModuleImage);
         TestUtils.AssertNewObjectReturnedEachTime(() => a.GetLoadedModules(getResourceModules: true));
         TestUtils.AssertNewObjectReturnedEachTime(() => a.GetLoadedModules(getResourceModules: false));
     }
 }
示例#14
0
 public static void AssemblyGetNameAlwaysReturnsNewInstance()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly     a     = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         AssemblyName name1 = a.GetName();
         AssemblyName name2 = a.GetName();
         Assert.NotSame(name1, name2);
     }
 }
示例#15
0
 public static void AssemblyEntryPoint1()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly   a = lc.LoadFromByteArray(TestData.s_AssemblyWithEntryPointImage);
         MethodInfo m = a.EntryPoint;
         Assert.Equal(TestData.s_AssemblyWithEntryPointEntryPointToken, m.MetadataToken);
         Assert.Equal("Main", m.Name);
     }
 }
示例#16
0
 public static void ModuleMvid()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a    = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m    = a.ManifestModule;
         Guid     mvid = m.ModuleVersionId;
         Assert.Equal(TestData.s_SimpleAssemblyMvid, mvid);
     }
 }
示例#17
0
        public static void NoResolver()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly derived = lc.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);
                Type     t       = derived.GetType("Derived1", throwOnError: true);

                Assert.Throws <FileNotFoundException>(() => t.BaseType);
            }
        }
示例#18
0
        public static void MetadataLoadContextDispensedObjectsAfterDispose()
        {
            Assembly a;

            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
            }

            Assert.Throws <ObjectDisposedException>(() => a.GetTypes());
        }
示例#19
0
 public static void AssemblyGetName()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         AssemblyName name = a.GetName(copiedName: false);
         Assert.NotNull(name);
         string fullName = name.FullName;
         Assert.Equal(TestData.s_SimpleAssemblyName, fullName);
     }
 }
示例#20
0
 public static void AssemblyGetCopiedName()
 {
     using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
     {
         Assembly     a    = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         AssemblyName name = a.GetName(copiedName: true);   // Shadow-copying is irrevant for MetadataLoadContext-loaded assemblies so this parameter is ignored.
         Assert.NotNull(name);
         string fullName = name.FullName;
         Assert.Equal(TestData.s_SimpleAssemblyFullName, fullName);
     }
 }
示例#21
0
        public static void AssemblyGetForwardedTypes3()
        {
            // Note this is using SimpleAssemblyResolver in order to resolve names between assemblies.
            using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
            {
                Assembly upper = lc.LoadFromByteArray(TestData.s_UpperImage);
                Assembly lower = lc.LoadFromByteArray(TestData.s_LowerImage);

                ReflectionTypeLoadException re = Assert.Throws <ReflectionTypeLoadException>(() => upper.GetForwardedTypesThunk());
                Assert.Equal(4, re.Types.Length);
                Assert.Equal(4, re.LoaderExceptions.Length);

                Assert.Equal(2, re.Types.Count((t) => t == null));
                Assert.Contains <Type>(lower.GetType("Upper3", throwOnError: true), re.Types);
                Assert.Contains <Type>(lower.GetType("Upper3+Upper3a", throwOnError: true), re.Types);

                Assert.Equal(2, re.LoaderExceptions.Count((t) => t == null));
                Assert.True(re.LoaderExceptions.All((t) => t == null || t is FileNotFoundException));
            }
        }
示例#22
0
        public static void CannotDoWithReflectionOnlyModule()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                // Storing as ICustomAttributeProvider so we don't accidentally pick up the CustomAttributeExtensions extension methods.
                ICustomAttributeProvider icp = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage).ManifestModule;

                Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(inherit: false));
                Assert.Throws <InvalidOperationException>(() => icp.GetCustomAttributes(null, inherit: false));
                Assert.Throws <InvalidOperationException>(() => icp.IsDefined(null, inherit: false));
            }
        }
示例#23
0
        public static void AssemblyGetExportedTypes1()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly upper     = lc.LoadFromByteArray(TestData.s_UpperImage);
                Type[]   types     = upper.GetExportedTypes().OrderBy(t => t.FullName).ToArray();
                string[] fullNames = types.Select(t => t.FullName).ToArray();

                string[] expected = { "Outer1", "Outer1+Inner1", "Upper1" };
                Assert.Equal <string>(expected, fullNames);
            }
        }
示例#24
0
        public static void AssemblyGetReferencedAssemblies()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly       a   = lc.LoadFromByteArray(TestData.s_AssemblyReferencesTestImage);
                AssemblyName[] ans = a.GetReferencedAssemblies();
                Assert.Equal(3, ans.Length);

                {
                    AssemblyName an = ans.Single(an2 => an2.Name == "mscorlib");
                    Assert.Equal(default(AssemblyNameFlags), an.Flags);
                    Version v = an.Version;
                    Assert.NotNull(v);
                    Assert.Equal(4, v.Major);
                    Assert.Equal(0, v.Minor);
                    Assert.Equal(0, v.Build);
                    Assert.Equal(0, v.Revision);
                    Assert.Equal(string.Empty, an.CultureName);
                    Assert.Null(an.GetPublicKey());
                    byte[] pkt         = an.GetPublicKeyToken();
                    byte[] expectedPkt = "b77a5c561934e089".HexToByteArray();
                    Assert.Equal <byte>(expectedPkt, pkt);
                }

                {
                    AssemblyName an = ans.Single(an2 => an2.Name == "dep1");
                    Assert.Equal(default(AssemblyNameFlags), 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);
                    Assert.Equal(string.Empty, an.CultureName);
                    Assert.Null(an.GetPublicKey());
                    Assert.Equal(0, an.GetPublicKeyToken().Length);
                }

                {
                    AssemblyName an = ans.Single(an2 => an2.Name == "dep2");
                    Assert.Equal(default(AssemblyNameFlags), an.Flags);
                    Version v = an.Version;
                    Assert.NotNull(v);
                    Assert.Equal(1, v.Major);
                    Assert.Equal(2, v.Minor);
                    Assert.Equal(3, v.Build);
                    Assert.Equal(4, v.Revision);
                    Assert.Equal("ar-LY", an.CultureName);
                    Assert.Null(an.GetPublicKey());
                    Assert.Equal(0, an.GetPublicKeyToken().Length);
                }
            }
        }
示例#25
0
        public static void AssemblyGetReferencedAssembliesReturnsDifferentObjectsEachTime()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly       a    = lc.LoadFromByteArray(TestData.s_AssemblyReferencesTestImage);
                AssemblyName[] ans1 = a.GetReferencedAssemblies();
                AssemblyName[] ans2 = a.GetReferencedAssemblies();

                Assert.Equal(ans1.Length, ans2.Length);
                if (ans1.Length != 0)
                {
                    Assert.NotSame(ans1, ans2);

                    // Be absolutely sure we're comparing apples to apples.
                    ans1 = ans1.OrderBy(an => an.Name).ToArray();
                    ans2 = ans2.OrderBy(an => an.Name).ToArray();

                    for (int i = 0; i < ans1.Length; i++)
                    {
                        Assert.NotSame(ans1[i], ans2[i]);

                        {
                            byte[] pk1 = ans1[i].GetPublicKey();
                            byte[] pk2 = ans2[i].GetPublicKey();
                            if (pk1 != null)
                            {
                                Assert.Equal <byte>(pk1, pk2);
                                if (pk1.Length != 0)
                                {
                                    Assert.NotSame(pk1, pk2);
                                }
                            }
                        }

                        {
                            byte[] pkt1 = ans1[i].GetPublicKeyToken();
                            byte[] pkt2 = ans2[i].GetPublicKeyToken();
                            if (pkt1 != null)
                            {
                                Assert.Equal <byte>(pkt1, pkt2);

                                if (pkt1.Length != 0)
                                {
                                    Assert.NotSame(pkt1, pkt2);
                                }
                            }
                        }
                    }
                }
            }
        }
        public static void LoadFromDifferentLocations()
        {
            using (TempFile tf1 = TempFile.Create(TestData.s_SimpleAssemblyImage))
                using (TempFile tf2 = TempFile.Create(TestData.s_SimpleAssemblyImage))
                    // Note this is using SimpleAssemblyResolver in order to resolve LoadFromAssemblyName.
                    using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
                    {
                        Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                        Assert.NotNull(a);

                        Assembly a1 = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                        Assert.Equal(a, a1);

                        Assembly a2 = lc.LoadFromAssemblyName(new AssemblyName(TestData.s_SimpleAssemblyFullName));
                        Assert.Equal(a, a2);

                        Assembly a3 = lc.LoadFromAssemblyPath(tf1.Path);
                        Assert.Equal(a, a3);

                        Assembly a4 = lc.LoadFromAssemblyPath(tf2.Path);
                        Assert.Equal(a, a4);
                    }
        }
        public static void AssemblyName_GetName_HashWithSha256()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
            {
                Assembly     a  = lc.LoadFromByteArray(TestData.s_HashWithSha256Image);
                AssemblyName an = a.GetName(copiedName: false);
                Assert.Equal("HashWithSha256", an.Name);

                Assert.Equal(AssemblyHashAlgorithm.SHA256, an.HashAlgorithm);

                Assembly aAgain = lc.LoadFromAssemblyName(an);
                Assert.Equal(a, aAgain);
            }
        }
示例#28
0
        public static void LoadFromDifferentLocations()
        {
            using (TempFile tf1 = TempFile.Create(TestData.s_SimpleAssemblyImage))
                using (TempFile tf2 = TempFile.Create(TestData.s_SimpleAssemblyImage))
                    using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
                    {
                        // As long as the MVID matches, you can load the same assembly from multiple locations.
                        Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                        Assert.NotNull(a);

                        Assembly a1 = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                        Assert.Equal(a, a1);

                        Assembly a2 = lc.LoadFromAssemblyName(new AssemblyName(TestData.s_SimpleAssemblyName));
                        Assert.Equal(a, a2);

                        Assembly a3 = lc.LoadFromAssemblyPath(tf1.Path);
                        Assert.Equal(a, a3);

                        Assembly a4 = lc.LoadFromAssemblyPath(tf2.Path);
                        Assert.Equal(a, a4);
                    }
        }
示例#29
0
        public static void MetadataLoadContextApisAfterDispose()
        {
            MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver());

            lc.Dispose();

            Assert.Throws <ObjectDisposedException>(() => lc.LoadFromAssemblyName(new AssemblyName("Foo")));
            Assert.Throws <ObjectDisposedException>(() => lc.LoadFromAssemblyName("Foo"));
            Assert.Throws <ObjectDisposedException>(() => lc.LoadFromAssemblyPath("Foo"));
            Assert.Throws <ObjectDisposedException>(() => lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage));
            Assert.Throws <ObjectDisposedException>(() => lc.LoadFromStream(new MemoryStream(TestData.s_SimpleAssemblyImage)));
            Assert.Throws <ObjectDisposedException>(() => lc.CoreAssembly);
            Assert.Throws <ObjectDisposedException>(() => lc.GetAssemblies());
        }
        public static void LoadFromDifferentLocationsMvidMismatch()
        {
            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                Assert.Equal(TestData.s_SimpleAssemblyFullName, a.GetName().FullName);
                Guid mvid = a.ManifestModule.ModuleVersionId;
                Assert.Equal(TestData.s_SimpleAssemblyMvid, mvid);
            }

            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyRecompiledImage);
                Assert.Equal(TestData.s_SimpleAssemblyFullName, a.GetName().FullName);
                Guid mvid = a.ManifestModule.ModuleVersionId;
                Assert.Equal(TestData.s_SimpleAssemblyRecompiledMvid, mvid);
            }

            using (MetadataLoadContext lc = new MetadataLoadContext(new EmptyCoreMetadataAssemblyResolver()))
            {
                Assembly a = lc.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                Assert.Throws <FileLoadException>(() => lc.LoadFromByteArray(TestData.s_SimpleAssemblyRecompiledImage));
            }
        }