Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / emit / xxxontypebuilderinstantiation.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 namespace System.Reflection.Emit
10 {
11     using System;
12     using System.Reflection;
13     using System.Collections;
14     using System.Collections.Generic;
15     using System.Globalization;
16     using System.Diagnostics.Contracts;
17
18     internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo
19     {
20         #region Private Static Members
21         internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type)
22         {
23             return new MethodOnTypeBuilderInstantiation(method, type);
24         }
25         #endregion
26
27         #region Private Data Mebers
28         internal MethodInfo m_method;
29         private TypeBuilderInstantiation m_type;
30         #endregion
31
32         #region Constructor
33         internal MethodOnTypeBuilderInstantiation(MethodInfo method, TypeBuilderInstantiation type)
34         {
35             Contract.Assert(method is MethodBuilder || method is RuntimeMethodInfo);
36
37             m_method = method;
38             m_type = type;
39         }
40         #endregion
41         
42         internal override Type[] GetParameterTypes()
43         {
44             return m_method.GetParameterTypes();
45         }
46
47         #region MemberInfo Overrides
48         public override MemberTypes MemberType { get { return m_method.MemberType;  } }
49         public override String Name { get { return m_method.Name; } }
50         public override Type DeclaringType { get { return m_type; } }
51         public override Type ReflectedType { get { return m_type; } }
52         public override Object[] GetCustomAttributes(bool inherit) { return m_method.GetCustomAttributes(inherit); } 
53         public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_method.GetCustomAttributes(attributeType, inherit); }
54         public override bool IsDefined(Type attributeType, bool inherit) { return m_method.IsDefined(attributeType, inherit); }
55         internal int MetadataTokenInternal
56         {
57             get
58             {
59                 MethodBuilder mb = m_method as MethodBuilder;
60
61                 if (mb != null)
62                     return mb.MetadataTokenInternal;
63                 else
64                 {
65                     Contract.Assert(m_method is RuntimeMethodInfo);
66                     return m_method.MetadataToken;
67                 }
68             }
69         }
70         public override Module Module { get { return m_method.Module; } }              
71         public new Type GetType() { return base.GetType(); }
72         #endregion
73
74         #region MethodBase Members
75         [Pure]
76         public override ParameterInfo[] GetParameters() { return m_method.GetParameters(); }
77         public override MethodImplAttributes GetMethodImplementationFlags() { return m_method.GetMethodImplementationFlags(); }
78         public override RuntimeMethodHandle MethodHandle { get { return m_method.MethodHandle; } }
79         public override MethodAttributes Attributes { get { return m_method.Attributes; } }
80         public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
81         {
82             throw new NotSupportedException();
83         }
84         public override CallingConventions CallingConvention { get { return m_method.CallingConvention; } }
85         public override Type [] GetGenericArguments() { return m_method.GetGenericArguments(); }
86         public override MethodInfo GetGenericMethodDefinition() { return m_method; }
87         public override bool IsGenericMethodDefinition { get { return m_method.IsGenericMethodDefinition; } }
88         public override bool ContainsGenericParameters { get { return m_method.ContainsGenericParameters; } }
89         public override MethodInfo MakeGenericMethod(params Type[] typeArgs)
90         {
91             if (!IsGenericMethodDefinition)
92                 throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericMethodDefinition"));
93             Contract.EndContractBlock();
94
95             return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs);
96         }
97
98         public override bool IsGenericMethod { get { return m_method.IsGenericMethod; } }
99       
100         #endregion
101
102         #region Public Abstract\Virtual Members
103         public override Type ReturnType { get { return m_method.ReturnType; } }
104         public override ParameterInfo ReturnParameter { get { throw new NotSupportedException(); } }
105         public override ICustomAttributeProvider ReturnTypeCustomAttributes { get { throw new NotSupportedException(); } }
106         public override MethodInfo GetBaseDefinition() { throw new NotSupportedException(); }
107         #endregion    
108     }
109
110     internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo
111     {
112         #region Private Static Members
113         internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type)
114         {
115             return new ConstructorOnTypeBuilderInstantiation(Constructor, type);
116         }
117         #endregion
118
119         #region Private Data Mebers
120         internal ConstructorInfo m_ctor;
121         private TypeBuilderInstantiation m_type;
122         #endregion
123
124         #region Constructor
125         internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type)
126         {
127             Contract.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo);
128
129             m_ctor = constructor;
130             m_type = type;
131         }
132         #endregion
133         
134         internal override Type[] GetParameterTypes()
135         {
136             return m_ctor.GetParameterTypes();
137         }
138
139         internal override Type GetReturnType()
140         {
141             return DeclaringType;
142         }
143
144         #region MemberInfo Overrides
145         public override MemberTypes MemberType { get { return m_ctor.MemberType; } }
146         public override String Name { get { return m_ctor.Name; } }
147         public override Type DeclaringType { get { return m_type; } }
148         public override Type ReflectedType { get { return m_type; } }
149         public override Object[] GetCustomAttributes(bool inherit) { return m_ctor.GetCustomAttributes(inherit); } 
150         public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_ctor.GetCustomAttributes(attributeType, inherit); }
151         public override bool IsDefined(Type attributeType, bool inherit) { return m_ctor.IsDefined(attributeType, inherit); }
152         internal int MetadataTokenInternal
153         {
154             get
155             {
156                 ConstructorBuilder cb = m_ctor as ConstructorBuilder;
157
158                 if (cb != null)
159                     return cb.MetadataTokenInternal;
160                 else
161                 {
162                     Contract.Assert(m_ctor is RuntimeConstructorInfo);
163                     return m_ctor.MetadataToken;
164                 }
165             }
166         }
167         public override Module Module { get { return m_ctor.Module; } }              
168         public new Type GetType() { return base.GetType(); }
169         #endregion
170
171         #region MethodBase Members
172         [Pure]
173         public override ParameterInfo[] GetParameters() { return m_ctor.GetParameters(); }
174         public override MethodImplAttributes GetMethodImplementationFlags() { return m_ctor.GetMethodImplementationFlags(); }
175         public override RuntimeMethodHandle MethodHandle { get { return m_ctor.MethodHandle; } }
176         public override MethodAttributes Attributes { get { return m_ctor.Attributes; } }
177         public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
178         {
179             throw new NotSupportedException();
180         }
181         public override CallingConventions CallingConvention { get { return m_ctor.CallingConvention; } }
182         public override Type[] GetGenericArguments() { return m_ctor.GetGenericArguments(); }
183         public override bool IsGenericMethodDefinition { get { return false; } }
184         public override bool ContainsGenericParameters { get { return false; } }
185
186         public override bool IsGenericMethod { get { return false; } }
187         #endregion
188
189         #region ConstructorInfo Members
190         public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
191         {
192             throw new InvalidOperationException();
193         }
194         #endregion
195     }
196
197     internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo
198     {
199         #region Private Static Members
200         internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type)
201         {
202             FieldInfo m = null;
203
204             // This ifdef was introduced when non-generic collections were pulled from
205             // silverlight. See code:Dictionary#DictionaryVersusHashtableThreadSafety
206             // for information about this change.
207             //
208             // There is a pre-existing race condition in this code with the side effect
209             // that the second thread's value clobbers the first in the hashtable. This is 
210             // an acceptable ---- since we make no guarantees that this will return the
211             // same object.
212             //
213             // We're not entirely sure if this cache helps any specific scenarios, so 
214             // long-term, one could investigate whether it's needed. In any case, this
215             // method isn't expected to be on any critical paths for performance.
216             if (type.m_hashtable.Contains(Field)) {
217                 m = type.m_hashtable[Field] as FieldInfo;
218             }
219             else {
220                 m = new FieldOnTypeBuilderInstantiation(Field, type);
221                 type.m_hashtable[Field] = m;
222             }
223
224             return m;
225         }
226         #endregion
227
228         #region Private Data Members
229         private FieldInfo m_field;
230         private TypeBuilderInstantiation m_type;
231         #endregion
232
233         #region Constructor
234         internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type)
235         {
236             Contract.Assert(field is FieldBuilder || field is RuntimeFieldInfo);
237
238             m_field = field;
239             m_type = type;
240         }
241         #endregion
242
243         internal FieldInfo FieldInfo { get { return m_field; } }
244
245         #region MemberInfo Overrides
246         public override MemberTypes MemberType { get { return System.Reflection.MemberTypes.Field; } }          
247         public override String Name { get { return m_field.Name; } }
248         public override Type DeclaringType { get { return m_type; } }
249         public override Type ReflectedType { get { return m_type; } }
250         public override Object[] GetCustomAttributes(bool inherit) { return m_field.GetCustomAttributes(inherit); } 
251         public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { return m_field.GetCustomAttributes(attributeType, inherit); }
252         public override bool IsDefined(Type attributeType, bool inherit) { return m_field.IsDefined(attributeType, inherit); }
253         internal int MetadataTokenInternal
254         {
255             get
256             {
257                 FieldBuilder fb = m_field as FieldBuilder;
258
259                 if (fb != null)
260                     return fb.MetadataTokenInternal;
261                 else
262                 {
263                     Contract.Assert(m_field is RuntimeFieldInfo);
264                     return m_field.MetadataToken;
265                 }
266             }
267         }
268         public override Module Module { get { return m_field.Module; } }              
269         public new Type GetType() { return base.GetType(); }
270         #endregion
271
272         #region Public Abstract\Virtual Members
273         public override Type[] GetRequiredCustomModifiers() { return m_field.GetRequiredCustomModifiers(); }
274         public override Type[] GetOptionalCustomModifiers() { return m_field.GetOptionalCustomModifiers(); }
275         public override void SetValueDirect(TypedReference obj, Object value)
276         {
277             throw new NotImplementedException();
278         }
279         public override Object GetValueDirect(TypedReference obj)
280         {
281             throw new NotImplementedException();
282         }    
283         public override RuntimeFieldHandle FieldHandle 
284         {
285             get { throw new NotImplementedException(); }
286         }    
287         public override Type FieldType { get { throw new NotImplementedException(); } }
288         public override Object GetValue(Object obj) { throw new InvalidOperationException(); }
289         public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture) { throw new InvalidOperationException(); }
290         public override FieldAttributes Attributes { get { return m_field.Attributes;  } }
291         #endregion
292     }
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333