Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Assembly_Members.cs « Assembly « tests « System.Reflection « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a39e45010785e43820af3ff2ec920b9b5f2d1345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace System.Reflection.Tests
{
    public class Assembly_Members
    {
        [Fact]
        public void CodeBase()
        {
            string codeBase = GetExecutingAssembly().CodeBase;
            Assert.False(string.IsNullOrEmpty(codeBase));
        }

        [Fact]
        public void ImageRuntimeVersion()
        {
            string ver = GetExecutingAssembly().ImageRuntimeVersion;
            Assert.False(string.IsNullOrEmpty(ver));
        }

        [Fact]
        public void CreateInstance()
        {
            Assert.Throws<ArgumentException>(() => GetExecutingAssembly().CreateInstance(""));
            Assert.Throws<ArgumentNullException>(() => GetExecutingAssembly().CreateInstance(null));
            Assert.Throws<MissingMethodException>(() => GetExecutingAssembly().CreateInstance(typeof(MyClassWithPrivateCtor).FullName));
            Assert.Throws<MissingMethodException>(() => GetExecutingAssembly().CreateInstance(typeof(MyClassNoDefaultCtor).FullName));

            Object obj = GetExecutingAssembly().CreateInstance(typeof(MyClass).FullName);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(MyClass));

            obj = typeof(int).GetTypeInfo().Assembly.CreateInstance(typeof(int).FullName);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(int));

            obj = typeof(int).GetTypeInfo().Assembly.CreateInstance(typeof(Dictionary<int, string>).FullName);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(Dictionary<int, string>));
        }

        [Fact]
        public void CreateInstance_IgnoreCase()
        {
            Assert.Throws<ArgumentException>(() => GetExecutingAssembly().CreateInstance("", false));
            Assert.Throws<ArgumentNullException>(() => GetExecutingAssembly().CreateInstance(null, true));
            Assert.Throws<MissingMethodException>(() => GetExecutingAssembly().CreateInstance(typeof(MyClassWithPrivateCtor).FullName, false));
            Assert.Throws<MissingMethodException>(() => GetExecutingAssembly().CreateInstance(typeof(MyClassNoDefaultCtor).FullName, true));

            Object obj = GetExecutingAssembly().CreateInstance(typeof(MyClass).FullName, false);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(MyClass));

            obj = GetExecutingAssembly().CreateInstance(typeof(MyClass).FullName.ToLower(), true);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(MyClass));

            obj = typeof(int).GetTypeInfo().Assembly.CreateInstance(typeof(int).FullName.ToLower(), true);
            Assert.NotNull(obj);
            Assert.Equal(obj.GetType(), typeof(int));

            obj = typeof(Dictionary<,>).GetTypeInfo().Assembly.CreateInstance(typeof(Dictionary<int, string>).FullName.ToUpper(), true);
            Assert.NotNull(obj);
            Assert.Equal(typeof(Dictionary<int, string>), obj.GetType());
        }

        [Fact]
        public static void CreateQualifiedName()
        {
            Assert.Equal(typeof(Assembly_Members).FullName + ", " + GetExecutingAssembly().ToString(), 
                Assembly.CreateQualifiedName(GetExecutingAssembly().ToString(), typeof(Assembly_Members).FullName));
        }

        [Fact]
        public static void GetExportedTypes()
        {
            Type[] types = GetExecutingAssembly().GetExportedTypes();
            Assert.NotNull(types);
            Assert.True(types.Length > 0);
        }

        [Fact]
        public static void GetReferencedAssemblies()
        {
            // It is too brittle to depend on the assembly references so we simply call the method and check that it does not throw.
            AssemblyName[] assemblies = GetExecutingAssembly().GetReferencedAssemblies();
            Assert.NotNull(assemblies);
            Assert.True(assemblies.Length > 0);
        }

        [Fact]
        public static void GetTypeMethod()
        {
            Type type = GetExecutingAssembly().GetType(typeof(MyClass).FullName);
            Assert.NotNull(type);
            Assert.Equal(type, typeof(MyClass));

            type = GetExecutingAssembly().GetType(typeof(MyPrivateNestedClass).FullName);
            Assert.NotNull(type);
            Assert.Equal(type, typeof(MyPrivateNestedClass));

            type = GetExecutingAssembly().GetType(typeof(MyPrivateClass).FullName);
            Assert.NotNull(type);
            Assert.Equal(type, typeof(MyPrivateClass));

            type = GetExecutingAssembly().GetType(typeof(MyClassWithPrivateCtor).FullName, false);
            Assert.NotNull(type);
            Assert.Equal(type, typeof(MyClassWithPrivateCtor));

            type = GetExecutingAssembly().GetType(typeof(MyClassNoDefaultCtor).FullName, true);
            Assert.NotNull(type);
            Assert.Equal(type, typeof(MyClassNoDefaultCtor));

            type = GetExecutingAssembly().GetType("notfound", false);
            Assert.Null(type);

            Assert.Throws<TypeLoadException>(() => GetExecutingAssembly().GetType("notfound", true));
        }

        public static Assembly GetExecutingAssembly()
        {
            Assembly asm = null;
            Type t = typeof(Assembly_Members);
            TypeInfo ti = t.GetTypeInfo();
            asm = ti.Assembly;

            return asm;
        }

        public class MyClass
        {
            public MyClass() { }
        }

        public class MyClassWithPrivateCtor
        {
            private MyClassWithPrivateCtor() { }
        }

        public class MyClassNoDefaultCtor
        {
            public MyClassNoDefaultCtor(int x) { }
        }

        private class MyPrivateNestedClass{ }
    }

    class MyPrivateClass { }
}