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 :
60 #region Keep in sync with object-internals.h
61 #pragma warning disable 649
62 internal Type generic_type;
63 Type[] type_arguments;
65 #pragma warning restore 649
68 Hashtable fields, ctors, methods;
70 internal MonoGenericClass ()
72 // this should not be used
73 throw new InvalidOperationException ();
76 internal MonoGenericClass (Type tb, Type[] args)
78 this.generic_type = tb;
79 this.type_arguments = args;
81 This is a temporary hack until we can fix the rest of the runtime
82 to properly handle this class to be a complete UT.
84 We must not regisrer this with the runtime after the type is created
85 otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
86 which is very very broken.
88 if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
89 register_with_runtime (this);
93 internal override Type InternalResolve ()
95 Type gtd = generic_type.InternalResolve ();
96 Type[] args = new Type [type_arguments.Length];
97 for (int i = 0; i < type_arguments.Length; ++i)
98 args [i] = type_arguments [i].InternalResolve ();
99 return gtd.MakeGenericType (args);
102 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103 extern void initialize (FieldInfo[] fields);
105 [MethodImplAttribute(MethodImplOptions.InternalCall)]
106 internal static extern void register_with_runtime (Type type);
108 internal bool IsCreated {
110 TypeBuilder tb = generic_type as TypeBuilder;
111 return tb != null ? tb.is_created : true;
115 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
116 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
123 MonoGenericClass parent = GetParentType () as MonoGenericClass;
125 parent.initialize ();
127 initialize (generic_type.GetFields (flags));
132 Type GetParentType ()
134 return InflateType (generic_type.BaseType);
137 internal Type InflateType (Type type)
139 return InflateType (type, type_arguments, null);
142 internal Type InflateType (Type type, Type[] method_args)
144 return InflateType (type, type_arguments, method_args);
147 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
151 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
153 if (type.IsGenericParameter) {
154 if (type.DeclaringMethod == null)
155 return type_args == null ? type : type_args [type.GenericParameterPosition];
156 return method_args == null ? type : method_args [type.GenericParameterPosition];
159 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
161 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
163 if (type.GetArrayRank () > 1)
164 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
166 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
167 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
168 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
171 Type[] args = type.GetGenericArguments ();
172 for (int i = 0; i < args.Length; ++i)
173 args [i] = InflateType (args [i], type_args, method_args);
175 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
176 return gtd.MakeGenericType (args);
179 public override Type BaseType {
180 get { return generic_type.BaseType; }
183 public override Type[] GetInterfaces ()
185 throw new NotSupportedException ();
188 protected override bool IsValueTypeImpl ()
190 return generic_type.IsValueType;
193 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
198 methods = new Hashtable ();
199 if (!methods.ContainsKey (fromNoninstanciated))
200 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
201 return (MethodInfo)methods [fromNoninstanciated];
204 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
209 ctors = new Hashtable ();
210 if (!ctors.ContainsKey (fromNoninstanciated))
211 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
212 return (ConstructorInfo)ctors [fromNoninstanciated];
215 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
219 fields = new Hashtable ();
220 if (!fields.ContainsKey (fromNoninstanciated))
221 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
222 return (FieldInfo)fields [fromNoninstanciated];
225 public override MethodInfo[] GetMethods (BindingFlags bf)
227 throw new NotSupportedException ();
230 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
232 throw new NotSupportedException ();
235 public override FieldInfo[] GetFields (BindingFlags bf)
237 throw new NotSupportedException ();
240 public override PropertyInfo[] GetProperties (BindingFlags bf)
242 throw new NotSupportedException ();
245 public override EventInfo[] GetEvents (BindingFlags bf)
247 throw new NotSupportedException ();
250 public override Type[] GetNestedTypes (BindingFlags bf)
252 throw new NotSupportedException ();
255 public override bool IsAssignableFrom (Type c)
257 throw new NotSupportedException ();
260 public override Type UnderlyingSystemType {
264 public override Assembly Assembly {
265 get { return generic_type.Assembly; }
268 public override Module Module {
269 get { return generic_type.Module; }
272 public override string Name {
273 get { return generic_type.Name; }
276 public override string Namespace {
277 get { return generic_type.Namespace; }
280 public override string FullName {
281 get { return format_name (true, false); }
284 public override string AssemblyQualifiedName {
285 get { return format_name (true, true); }
288 public override Guid GUID {
289 get { throw new NotSupportedException (); }
292 string format_name (bool full_name, bool assembly_qualified)
294 StringBuilder sb = new StringBuilder (generic_type.FullName);
297 for (int i = 0; i < type_arguments.Length; ++i) {
303 string assemblyName = type_arguments [i].Assembly.FullName;
304 name = type_arguments [i].FullName;
305 if (name != null && assemblyName != null)
306 name = name + ", " + assemblyName;
308 name = type_arguments [i].ToString ();
320 if (assembly_qualified) {
322 sb.Append (generic_type.Assembly.FullName);
324 return sb.ToString ();
327 public override string ToString ()
329 return format_name (false, false);
332 public override Type GetGenericTypeDefinition ()
337 public override Type[] GetGenericArguments ()
339 Type[] ret = new Type [type_arguments.Length];
340 type_arguments.CopyTo (ret, 0);
344 public override bool ContainsGenericParameters {
346 foreach (Type t in type_arguments) {
347 if (t.ContainsGenericParameters)
354 public override bool IsGenericTypeDefinition {
355 get { return false; }
358 public override bool IsGenericType {
362 public override Type DeclaringType {
363 get { return generic_type.DeclaringType; }
366 public override RuntimeTypeHandle TypeHandle {
368 throw new NotSupportedException ();
372 public override Type MakeArrayType ()
374 return new ArrayType (this, 0);
377 public override Type MakeArrayType (int rank)
380 throw new IndexOutOfRangeException ();
381 return new ArrayType (this, rank);
384 public override Type MakeByRefType ()
386 return new ByRefType (this);
389 public override Type MakePointerType ()
391 return new PointerType (this);
394 public override Type GetElementType ()
396 throw new NotSupportedException ();
399 protected override bool HasElementTypeImpl ()
404 protected override bool IsCOMObjectImpl ()
409 protected override bool IsPrimitiveImpl ()
414 protected override bool IsArrayImpl ()
419 protected override bool IsByRefImpl ()
424 protected override bool IsPointerImpl ()
429 protected override TypeAttributes GetAttributeFlagsImpl ()
431 return generic_type.Attributes;
435 public override Type GetInterface (string name, bool ignoreCase)
437 throw new NotSupportedException ();
440 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
442 throw new NotSupportedException ();
445 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
447 throw new NotSupportedException ();
450 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
452 throw new NotSupportedException ();
455 public override Type GetNestedType (string name, BindingFlags bindingAttr)
457 throw new NotSupportedException ();
460 public override object InvokeMember (string name, BindingFlags invokeAttr,
461 Binder binder, object target, object[] args,
462 ParameterModifier[] modifiers,
463 CultureInfo culture, string[] namedParameters)
465 throw new NotSupportedException ();
468 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
469 CallingConventions callConvention, Type[] types,
470 ParameterModifier[] modifiers)
472 throw new NotSupportedException ();
475 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
476 Type returnType, Type[] types, ParameterModifier[] modifiers)
478 throw new NotSupportedException ();
481 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
483 CallingConventions callConvention,
485 ParameterModifier[] modifiers)
487 throw new NotSupportedException ();
491 public override bool IsDefined (Type attributeType, bool inherit)
493 throw new NotSupportedException ();
496 public override object [] GetCustomAttributes (bool inherit)
499 return generic_type.GetCustomAttributes (inherit);
500 throw new NotSupportedException ();
503 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
506 return generic_type.GetCustomAttributes (attributeType, inherit);
507 throw new NotSupportedException ();
510 internal override bool IsUserType {
512 foreach (var t in type_arguments) {