2 // System.Reflection.Emit.TypeBuilderInstantiation
4 // Sean MacIsaac (macisaac@ximian.com)
5 // Paolo Molaro (lupus@ximian.com)
6 // Patrik Torstensson (patrik.torstensson@labs2.com)
8 // (C) 2001 Ximian, Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Collections;
38 using System.Runtime.CompilerServices;
39 using System.Globalization;
40 using System.Runtime.Serialization;
42 using System.Runtime.InteropServices;
44 namespace System.Reflection.Emit
47 * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder.
49 [StructLayout (LayoutKind.Sequential)]
50 sealed class TypeBuilderInstantiation :
53 #region Keep in sync with object-internals.h
54 #pragma warning disable 649
55 internal Type generic_type;
56 Type[] type_arguments;
57 #pragma warning restore 649
60 Hashtable fields, ctors, methods;
62 internal TypeBuilderInstantiation ()
64 // this should not be used
65 throw new InvalidOperationException ();
68 internal TypeBuilderInstantiation (Type tb, Type[] args)
70 this.generic_type = tb;
71 this.type_arguments = args;
74 internal override Type InternalResolve ()
76 Type gtd = generic_type.InternalResolve ();
77 Type[] args = new Type [type_arguments.Length];
78 for (int i = 0; i < type_arguments.Length; ++i)
79 args [i] = type_arguments [i].InternalResolve ();
80 return gtd.MakeGenericType (args);
83 // Called from the runtime to return the corresponding finished Type object
84 internal override Type RuntimeResolve ()
86 if (generic_type is TypeBuilder && !(generic_type as TypeBuilder).IsCreated ())
87 AppDomain.CurrentDomain.DoTypeResolve (generic_type);
88 for (int i = 0; i < type_arguments.Length; ++i) {
89 var t = type_arguments [i];
90 if (t is TypeBuilder && !(t as TypeBuilder).IsCreated ())
91 AppDomain.CurrentDomain.DoTypeResolve (t);
93 return InternalResolve ();
96 internal bool IsCreated {
98 TypeBuilder tb = generic_type as TypeBuilder;
99 return tb != null ? tb.is_created : true;
103 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
104 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
106 Type GetParentType ()
108 return InflateType (generic_type.BaseType);
111 internal Type InflateType (Type type)
113 return InflateType (type, type_arguments, null);
116 internal Type InflateType (Type type, Type[] method_args)
118 return InflateType (type, type_arguments, method_args);
121 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
125 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
127 if (type.IsGenericParameter) {
128 if (type.DeclaringMethod == null)
129 return type_args == null ? type : type_args [type.GenericParameterPosition];
130 return method_args == null ? type : method_args [type.GenericParameterPosition];
133 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
135 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
137 if (type.GetArrayRank () > 1)
138 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
140 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
141 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
142 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
145 Type[] args = type.GetGenericArguments ();
146 for (int i = 0; i < args.Length; ++i)
147 args [i] = InflateType (args [i], type_args, method_args);
149 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
150 return gtd.MakeGenericType (args);
153 public override Type BaseType {
154 get { return generic_type.BaseType; }
157 public override Type[] GetInterfaces ()
159 throw new NotSupportedException ();
162 protected override bool IsValueTypeImpl ()
164 return generic_type.IsValueType;
167 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
170 methods = new Hashtable ();
171 if (!methods.ContainsKey (fromNoninstanciated))
172 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
173 return (MethodInfo)methods [fromNoninstanciated];
176 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
179 ctors = new Hashtable ();
180 if (!ctors.ContainsKey (fromNoninstanciated))
181 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
182 return (ConstructorInfo)ctors [fromNoninstanciated];
185 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
188 fields = new Hashtable ();
189 if (!fields.ContainsKey (fromNoninstanciated))
190 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
191 return (FieldInfo)fields [fromNoninstanciated];
194 public override MethodInfo[] GetMethods (BindingFlags bf)
196 throw new NotSupportedException ();
199 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
201 throw new NotSupportedException ();
204 public override FieldInfo[] GetFields (BindingFlags bf)
206 throw new NotSupportedException ();
209 public override PropertyInfo[] GetProperties (BindingFlags bf)
211 throw new NotSupportedException ();
214 public override EventInfo[] GetEvents (BindingFlags bf)
216 throw new NotSupportedException ();
219 public override Type[] GetNestedTypes (BindingFlags bf)
221 throw new NotSupportedException ();
224 public override bool IsAssignableFrom (Type c)
226 throw new NotSupportedException ();
229 public override Type UnderlyingSystemType {
233 public override Assembly Assembly {
234 get { return generic_type.Assembly; }
237 public override Module Module {
238 get { return generic_type.Module; }
241 public override string Name {
242 get { return generic_type.Name; }
245 public override string Namespace {
246 get { return generic_type.Namespace; }
249 public override string FullName {
250 get { return format_name (true, false); }
253 public override string AssemblyQualifiedName {
254 get { return format_name (true, true); }
257 public override Guid GUID {
258 get { throw new NotSupportedException (); }
261 string format_name (bool full_name, bool assembly_qualified)
263 StringBuilder sb = new StringBuilder (generic_type.FullName);
266 for (int i = 0; i < type_arguments.Length; ++i) {
272 string assemblyName = type_arguments [i].Assembly.FullName;
273 name = type_arguments [i].FullName;
274 if (name != null && assemblyName != null)
275 name = name + ", " + assemblyName;
277 name = type_arguments [i].ToString ();
289 if (assembly_qualified) {
291 sb.Append (generic_type.Assembly.FullName);
293 return sb.ToString ();
296 public override string ToString ()
298 return format_name (false, false);
301 public override Type GetGenericTypeDefinition ()
306 public override Type[] GetGenericArguments ()
308 Type[] ret = new Type [type_arguments.Length];
309 type_arguments.CopyTo (ret, 0);
313 public override bool ContainsGenericParameters {
315 foreach (Type t in type_arguments) {
316 if (t.ContainsGenericParameters)
323 public override bool IsGenericTypeDefinition {
324 get { return false; }
327 public override bool IsGenericType {
331 public override Type DeclaringType {
332 get { return generic_type.DeclaringType; }
335 public override RuntimeTypeHandle TypeHandle {
337 throw new NotSupportedException ();
341 public override Type MakeArrayType ()
343 return new ArrayType (this, 0);
346 public override Type MakeArrayType (int rank)
349 throw new IndexOutOfRangeException ();
350 return new ArrayType (this, rank);
353 public override Type MakeByRefType ()
355 return new ByRefType (this);
358 public override Type MakePointerType ()
360 return new PointerType (this);
363 public override Type GetElementType ()
365 throw new NotSupportedException ();
368 protected override bool HasElementTypeImpl ()
373 protected override bool IsCOMObjectImpl ()
378 protected override bool IsPrimitiveImpl ()
383 protected override bool IsArrayImpl ()
388 protected override bool IsByRefImpl ()
393 protected override bool IsPointerImpl ()
398 protected override TypeAttributes GetAttributeFlagsImpl ()
400 return generic_type.Attributes;
404 public override Type GetInterface (string name, bool ignoreCase)
406 throw new NotSupportedException ();
409 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
411 throw new NotSupportedException ();
414 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
416 throw new NotSupportedException ();
419 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
421 throw new NotSupportedException ();
424 public override Type GetNestedType (string name, BindingFlags bindingAttr)
426 throw new NotSupportedException ();
429 public override object InvokeMember (string name, BindingFlags invokeAttr,
430 Binder binder, object target, object[] args,
431 ParameterModifier[] modifiers,
432 CultureInfo culture, string[] namedParameters)
434 throw new NotSupportedException ();
437 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
438 CallingConventions callConvention, Type[] types,
439 ParameterModifier[] modifiers)
441 throw new NotSupportedException ();
444 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
445 Type returnType, Type[] types, ParameterModifier[] modifiers)
447 throw new NotSupportedException ();
450 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
452 CallingConventions callConvention,
454 ParameterModifier[] modifiers)
456 throw new NotSupportedException ();
460 public override bool IsDefined (Type attributeType, bool inherit)
462 throw new NotSupportedException ();
465 public override object [] GetCustomAttributes (bool inherit)
468 return generic_type.GetCustomAttributes (inherit);
469 throw new NotSupportedException ();
472 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
475 return generic_type.GetCustomAttributes (attributeType, inherit);
476 throw new NotSupportedException ();
479 internal override bool IsUserType {
481 foreach (var t in type_arguments) {
489 internal static Type MakeGenericType (Type type, Type[] typeArguments)
491 return new TypeBuilderInstantiation (type, typeArguments);
496 namespace System.Reflection.Emit
498 abstract class TypeBuilderInstantiation : TypeInfo
500 internal static Type MakeGenericType (Type type, Type[] typeArguments)
502 throw new NotSupportedException ("User types are not supported under full aot");