3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
9 namespace System.Reflection.Emit
12 using System.Reflection;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Globalization;
16 using System.Diagnostics.Contracts;
18 internal sealed class MethodOnTypeBuilderInstantiation : MethodInfo
20 #region Private Static Members
21 internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation type)
23 return new MethodOnTypeBuilderInstantiation(method, type);
27 #region Private Data Mebers
28 internal MethodInfo m_method;
29 private TypeBuilderInstantiation m_type;
33 internal MethodOnTypeBuilderInstantiation(MethodInfo method, TypeBuilderInstantiation type)
35 Contract.Assert(method is MethodBuilder || method is RuntimeMethodInfo);
42 internal override Type[] GetParameterTypes()
44 return m_method.GetParameterTypes();
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
59 MethodBuilder mb = m_method as MethodBuilder;
62 return mb.MetadataTokenInternal;
65 Contract.Assert(m_method is RuntimeMethodInfo);
66 return m_method.MetadataToken;
70 public override Module Module { get { return m_method.Module; } }
71 public new Type GetType() { return base.GetType(); }
74 #region MethodBase Members
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)
82 throw new NotSupportedException();
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)
91 if (!IsGenericMethodDefinition)
92 throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericMethodDefinition"));
93 Contract.EndContractBlock();
95 return MethodBuilderInstantiation.MakeGenericMethod(this, typeArgs);
98 public override bool IsGenericMethod { get { return m_method.IsGenericMethod; } }
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(); }
110 internal sealed class ConstructorOnTypeBuilderInstantiation : ConstructorInfo
112 #region Private Static Members
113 internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type)
115 return new ConstructorOnTypeBuilderInstantiation(Constructor, type);
119 #region Private Data Mebers
120 internal ConstructorInfo m_ctor;
121 private TypeBuilderInstantiation m_type;
125 internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type)
127 Contract.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo);
129 m_ctor = constructor;
134 internal override Type[] GetParameterTypes()
136 return m_ctor.GetParameterTypes();
139 internal override Type GetReturnType()
141 return DeclaringType;
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
156 ConstructorBuilder cb = m_ctor as ConstructorBuilder;
159 return cb.MetadataTokenInternal;
162 Contract.Assert(m_ctor is RuntimeConstructorInfo);
163 return m_ctor.MetadataToken;
167 public override Module Module { get { return m_ctor.Module; } }
168 public new Type GetType() { return base.GetType(); }
171 #region MethodBase Members
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)
179 throw new NotSupportedException();
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; } }
186 public override bool IsGenericMethod { get { return false; } }
189 #region ConstructorInfo Members
190 public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
192 throw new InvalidOperationException();
197 internal sealed class FieldOnTypeBuilderInstantiation : FieldInfo
199 #region Private Static Members
200 internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation type)
204 // This ifdef was introduced when non-generic collections were pulled from
205 // silverlight. See code:Dictionary#DictionaryVersusHashtableThreadSafety
206 // for information about this change.
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
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;
220 m = new FieldOnTypeBuilderInstantiation(Field, type);
221 type.m_hashtable[Field] = m;
228 #region Private Data Members
229 private FieldInfo m_field;
230 private TypeBuilderInstantiation m_type;
234 internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type)
236 Contract.Assert(field is FieldBuilder || field is RuntimeFieldInfo);
243 internal FieldInfo FieldInfo { get { return m_field; } }
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
257 FieldBuilder fb = m_field as FieldBuilder;
260 return fb.MetadataTokenInternal;
263 Contract.Assert(m_field is RuntimeFieldInfo);
264 return m_field.MetadataToken;
268 public override Module Module { get { return m_field.Module; } }
269 public new Type GetType() { return base.GetType(); }
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)
277 throw new NotImplementedException();
279 public override Object GetValueDirect(TypedReference obj)
281 throw new NotImplementedException();
283 public override RuntimeFieldHandle FieldHandle
285 get { throw new NotImplementedException(); }
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; } }