2 // System.Reflection.MonoGenericClass
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
47 * MonoGenericClass represents an instantiation of a generic TypeBuilder. MS
48 * calls this class TypeBuilderInstantiation (a much better name). MS returns
49 * NotImplementedException for many of the methods but we can't do that as gmcs
52 [StructLayout (LayoutKind.Sequential)]
53 sealed class MonoGenericClass :
56 #region Keep in sync with object-internals.h
57 #pragma warning disable 649
58 internal Type generic_type;
59 Type[] type_arguments;
60 #pragma warning restore 649
63 Hashtable fields, ctors, methods;
65 internal MonoGenericClass ()
67 // this should not be used
68 throw new InvalidOperationException ();
71 internal MonoGenericClass (Type tb, Type[] args)
73 this.generic_type = tb;
74 this.type_arguments = args;
77 internal override Type InternalResolve ()
79 Type gtd = generic_type.InternalResolve ();
80 Type[] args = new Type [type_arguments.Length];
81 for (int i = 0; i < type_arguments.Length; ++i)
82 args [i] = type_arguments [i].InternalResolve ();
83 return gtd.MakeGenericType (args);
86 // Called from the runtime to return the corresponding finished Type object
87 internal override Type RuntimeResolve ()
89 if (generic_type is TypeBuilder && !(generic_type as TypeBuilder).IsCreated ())
90 AppDomain.CurrentDomain.DoTypeResolve (generic_type);
91 for (int i = 0; i < type_arguments.Length; ++i) {
92 var t = type_arguments [i];
93 if (t is TypeBuilder && !(t as TypeBuilder).IsCreated ())
94 AppDomain.CurrentDomain.DoTypeResolve (t);
96 return InternalResolve ();
99 internal bool IsCreated {
101 TypeBuilder tb = generic_type as TypeBuilder;
102 return tb != null ? tb.is_created : true;
106 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
107 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
109 Type GetParentType ()
111 return InflateType (generic_type.BaseType);
114 internal Type InflateType (Type type)
116 return InflateType (type, type_arguments, null);
119 internal Type InflateType (Type type, Type[] method_args)
121 return InflateType (type, type_arguments, method_args);
124 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
128 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
130 if (type.IsGenericParameter) {
131 if (type.DeclaringMethod == null)
132 return type_args == null ? type : type_args [type.GenericParameterPosition];
133 return method_args == null ? type : method_args [type.GenericParameterPosition];
136 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
138 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
140 if (type.GetArrayRank () > 1)
141 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
143 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
144 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
145 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
148 Type[] args = type.GetGenericArguments ();
149 for (int i = 0; i < args.Length; ++i)
150 args [i] = InflateType (args [i], type_args, method_args);
152 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
153 return gtd.MakeGenericType (args);
156 public override Type BaseType {
157 get { return generic_type.BaseType; }
160 public override Type[] GetInterfaces ()
162 throw new NotSupportedException ();
165 protected override bool IsValueTypeImpl ()
167 return generic_type.IsValueType;
170 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
173 methods = new Hashtable ();
174 if (!methods.ContainsKey (fromNoninstanciated))
175 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
176 return (MethodInfo)methods [fromNoninstanciated];
179 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
182 ctors = new Hashtable ();
183 if (!ctors.ContainsKey (fromNoninstanciated))
184 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
185 return (ConstructorInfo)ctors [fromNoninstanciated];
188 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
191 fields = new Hashtable ();
192 if (!fields.ContainsKey (fromNoninstanciated))
193 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
194 return (FieldInfo)fields [fromNoninstanciated];
197 public override MethodInfo[] GetMethods (BindingFlags bf)
199 throw new NotSupportedException ();
202 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
204 throw new NotSupportedException ();
207 public override FieldInfo[] GetFields (BindingFlags bf)
209 throw new NotSupportedException ();
212 public override PropertyInfo[] GetProperties (BindingFlags bf)
214 throw new NotSupportedException ();
217 public override EventInfo[] GetEvents (BindingFlags bf)
219 throw new NotSupportedException ();
222 public override Type[] GetNestedTypes (BindingFlags bf)
224 throw new NotSupportedException ();
227 public override bool IsAssignableFrom (Type c)
229 throw new NotSupportedException ();
232 public override Type UnderlyingSystemType {
236 public override Assembly Assembly {
237 get { return generic_type.Assembly; }
240 public override Module Module {
241 get { return generic_type.Module; }
244 public override string Name {
245 get { return generic_type.Name; }
248 public override string Namespace {
249 get { return generic_type.Namespace; }
252 public override string FullName {
253 get { return format_name (true, false); }
256 public override string AssemblyQualifiedName {
257 get { return format_name (true, true); }
260 public override Guid GUID {
261 get { throw new NotSupportedException (); }
264 string format_name (bool full_name, bool assembly_qualified)
266 StringBuilder sb = new StringBuilder (generic_type.FullName);
269 for (int i = 0; i < type_arguments.Length; ++i) {
275 string assemblyName = type_arguments [i].Assembly.FullName;
276 name = type_arguments [i].FullName;
277 if (name != null && assemblyName != null)
278 name = name + ", " + assemblyName;
280 name = type_arguments [i].ToString ();
292 if (assembly_qualified) {
294 sb.Append (generic_type.Assembly.FullName);
296 return sb.ToString ();
299 public override string ToString ()
301 return format_name (false, false);
304 public override Type GetGenericTypeDefinition ()
309 public override Type[] GetGenericArguments ()
311 Type[] ret = new Type [type_arguments.Length];
312 type_arguments.CopyTo (ret, 0);
316 public override bool ContainsGenericParameters {
318 foreach (Type t in type_arguments) {
319 if (t.ContainsGenericParameters)
326 public override bool IsGenericTypeDefinition {
327 get { return false; }
330 public override bool IsGenericType {
334 public override Type DeclaringType {
335 get { return generic_type.DeclaringType; }
338 public override RuntimeTypeHandle TypeHandle {
340 throw new NotSupportedException ();
344 public override Type MakeArrayType ()
346 return new ArrayType (this, 0);
349 public override Type MakeArrayType (int rank)
352 throw new IndexOutOfRangeException ();
353 return new ArrayType (this, rank);
356 public override Type MakeByRefType ()
358 return new ByRefType (this);
361 public override Type MakePointerType ()
363 return new PointerType (this);
366 public override Type GetElementType ()
368 throw new NotSupportedException ();
371 protected override bool HasElementTypeImpl ()
376 protected override bool IsCOMObjectImpl ()
381 protected override bool IsPrimitiveImpl ()
386 protected override bool IsArrayImpl ()
391 protected override bool IsByRefImpl ()
396 protected override bool IsPointerImpl ()
401 protected override TypeAttributes GetAttributeFlagsImpl ()
403 return generic_type.Attributes;
407 public override Type GetInterface (string name, bool ignoreCase)
409 throw new NotSupportedException ();
412 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
414 throw new NotSupportedException ();
417 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
419 throw new NotSupportedException ();
422 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
424 throw new NotSupportedException ();
427 public override Type GetNestedType (string name, BindingFlags bindingAttr)
429 throw new NotSupportedException ();
432 public override object InvokeMember (string name, BindingFlags invokeAttr,
433 Binder binder, object target, object[] args,
434 ParameterModifier[] modifiers,
435 CultureInfo culture, string[] namedParameters)
437 throw new NotSupportedException ();
440 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
441 CallingConventions callConvention, Type[] types,
442 ParameterModifier[] modifiers)
444 throw new NotSupportedException ();
447 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
448 Type returnType, Type[] types, ParameterModifier[] modifiers)
450 throw new NotSupportedException ();
453 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
455 CallingConventions callConvention,
457 ParameterModifier[] modifiers)
459 throw new NotSupportedException ();
463 public override bool IsDefined (Type attributeType, bool inherit)
465 throw new NotSupportedException ();
468 public override object [] GetCustomAttributes (bool inherit)
471 return generic_type.GetCustomAttributes (inherit);
472 throw new NotSupportedException ();
475 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
478 return generic_type.GetCustomAttributes (attributeType, inherit);
479 throw new NotSupportedException ();
482 internal override bool IsUserType {
484 foreach (var t in type_arguments) {