summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
blob: 64a38b0995eaf643cdd8e9c0570e7014a578c3e4 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// 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 System;
using System.Reflection;
using System.Collections;
using System.Globalization;
using System.Diagnostics.Contracts;

namespace System.Reflection.Emit
{
    internal sealed class TypeBuilderInstantiation : TypeInfo
    {
        public override bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo)
        {
            if (typeInfo == null) return false;
            return IsAssignableFrom(typeInfo.AsType());
        }

        #region Static Members
        internal static Type MakeGenericType(Type type, Type[] typeArguments)
        {
            Contract.Requires(type != null, "this is only called from RuntimeType.MakeGenericType and TypeBuilder.MakeGenericType so 'type' cannot be null");

            if (!type.IsGenericTypeDefinition)
                throw new InvalidOperationException();

            if (typeArguments == null)
                throw new ArgumentNullException(nameof(typeArguments));
            Contract.EndContractBlock();

            foreach (Type t in typeArguments)
            {
                if (t == null)
                    throw new ArgumentNullException(nameof(typeArguments));
            }

            return new TypeBuilderInstantiation(type, typeArguments);
        }

        #endregion

        #region Private Data Mebers
        private Type m_type;
        private Type[] m_inst;
        private string m_strFullQualName;
        internal Hashtable m_hashtable = new Hashtable();

        #endregion

        #region Constructor
        private TypeBuilderInstantiation(Type type, Type[] inst)
        {
            m_type = type;
            m_inst = inst;
            m_hashtable = new Hashtable();
        }
        #endregion

        #region Object Overrides
        public override String ToString()
        {
            return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString);
        }
        #endregion

        #region MemberInfo Overrides
        public override Type DeclaringType { get { return m_type.DeclaringType; } }

        public override Type ReflectedType { get { return m_type.ReflectedType; } }

        public override String Name { get { return m_type.Name; } }

        public override Module Module { get { return m_type.Module; } }
        #endregion

        #region Type Overrides
        public override Type MakePointerType()
        {
            return SymbolType.FormCompoundType("*", this, 0);
        }
        public override Type MakeByRefType()
        {
            return SymbolType.FormCompoundType("&", this, 0);
        }
        public override Type MakeArrayType()
        {
            return SymbolType.FormCompoundType("[]", this, 0);
        }
        public override Type MakeArrayType(int rank)
        {
            if (rank <= 0)
                throw new IndexOutOfRangeException();
            Contract.EndContractBlock();

            string comma = "";
            for (int i = 1; i < rank; i++)
                comma += ",";

            string s = String.Format(CultureInfo.InvariantCulture, "[{0}]", comma);
            return SymbolType.FormCompoundType(s, this, 0);
        }
        public override Guid GUID { get { throw new NotSupportedException(); } }
        public override Object InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) { throw new NotSupportedException(); }
        public override Assembly Assembly { get { return m_type.Assembly; } }
        public override RuntimeTypeHandle TypeHandle { get { throw new NotSupportedException(); } }
        public override String FullName
        {
            get
            {
                if (m_strFullQualName == null)
                    m_strFullQualName = TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName);
                return m_strFullQualName;
            }
        }
        public override String Namespace { get { return m_type.Namespace; } }
        public override String AssemblyQualifiedName { get { return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName); } }
        private Type Substitute(Type[] substitutes)
        {
            Type[] inst = GetGenericArguments();
            Type[] instSubstituted = new Type[inst.Length];

            for (int i = 0; i < instSubstituted.Length; i++)
            {
                Type t = inst[i];

                if (t is TypeBuilderInstantiation)
                {
                    instSubstituted[i] = (t as TypeBuilderInstantiation).Substitute(substitutes);
                }
                else if (t is GenericTypeParameterBuilder)
                {
                    // Substitute
                    instSubstituted[i] = substitutes[t.GenericParameterPosition];
                }
                else
                {
                    instSubstituted[i] = t;
                }
            }

            return GetGenericTypeDefinition().MakeGenericType(instSubstituted);
        }
        public override Type BaseType
        {
            // B<A,B,C>
            // D<T,S> : B<S,List<T>,char>

            // D<string,int> : B<int,List<string>,char>
            // D<S,T> : B<T,List<S>,char>        
            // D<S,string> : B<string,List<S>,char>        
            get
            {
                Type typeBldrBase = m_type.BaseType;

                if (typeBldrBase == null)
                    return null;

                TypeBuilderInstantiation typeBldrBaseAs = typeBldrBase as TypeBuilderInstantiation;

                if (typeBldrBaseAs == null)
                    return typeBldrBase;

                return typeBldrBaseAs.Substitute(GetGenericArguments());
            }
        }
        protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) { throw new NotSupportedException(); }

        public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        protected override MethodInfo GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) { throw new NotSupportedException(); }
        public override MethodInfo[] GetMethods(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override FieldInfo GetField(String name, BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override FieldInfo[] GetFields(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override Type GetInterface(String name, bool ignoreCase) { throw new NotSupportedException(); }
        public override Type[] GetInterfaces() { throw new NotSupportedException(); }
        public override EventInfo GetEvent(String name, BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override EventInfo[] GetEvents() { throw new NotSupportedException(); }
        protected override PropertyInfo GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) { throw new NotSupportedException(); }
        public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override Type[] GetNestedTypes(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override Type GetNestedType(String name, BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override MemberInfo[] GetMember(String name, MemberTypes type, BindingFlags bindingAttr) { throw new NotSupportedException(); }

        public override InterfaceMapping GetInterfaceMap(Type interfaceType) { throw new NotSupportedException(); }
        public override EventInfo[] GetEvents(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); }
        protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; }

        public override bool IsTypeDefinition => false;
        public override bool IsSZArray => false;

        protected override bool IsArrayImpl() { return false; }
        protected override bool IsByRefImpl() { return false; }
        protected override bool IsPointerImpl() { return false; }
        protected override bool IsPrimitiveImpl() { return false; }
        protected override bool IsCOMObjectImpl() { return false; }
        public override Type GetElementType() { throw new NotSupportedException(); }
        protected override bool HasElementTypeImpl() { return false; }
        public override Type UnderlyingSystemType { get { return this; } }
        public override Type[] GetGenericArguments() { return m_inst; }
        public override bool IsGenericTypeDefinition { get { return false; } }
        public override bool IsGenericType { get { return true; } }
        public override bool IsConstructedGenericType { get { return true; } }
        public override bool IsGenericParameter { get { return false; } }
        public override int GenericParameterPosition { get { throw new InvalidOperationException(); } }
        protected override bool IsValueTypeImpl() { return m_type.IsValueType; }
        public override bool ContainsGenericParameters
        {
            get
            {
                for (int i = 0; i < m_inst.Length; i++)
                {
                    if (m_inst[i].ContainsGenericParameters)
                        return true;
                }

                return false;
            }
        }
        public override MethodBase DeclaringMethod { get { return null; } }
        public override Type GetGenericTypeDefinition() { return m_type; }
        public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Arg_NotGenericTypeDefinition); }
        public override bool IsAssignableFrom(Type c) { throw new NotSupportedException(); }

        [Pure]
        public override bool IsSubclassOf(Type c)
        {
            throw new NotSupportedException();
        }
        #endregion

        #region ICustomAttributeProvider Implementation
        public override Object[] GetCustomAttributes(bool inherit) { throw new NotSupportedException(); }

        public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotSupportedException(); }

        public override bool IsDefined(Type attributeType, bool inherit) { throw new NotSupportedException(); }
        #endregion
    }
}