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.
34 using System.Reflection;
35 using System.Reflection.Emit;
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Globalization;
39 using System.Runtime.Serialization;
41 using System.Runtime.InteropServices;
43 namespace System.Reflection
46 * MonoGenericClass represents an instantiation of a generic TypeBuilder. MS
47 * calls this class TypeBuilderInstantiation (a much better name). MS returns
48 * NotImplementedException for many of the methods but we can't do that as gmcs
51 [StructLayout (LayoutKind.Sequential)]
52 internal class MonoGenericClass : Type
54 #region Keep in sync with object-internals.h
55 #pragma warning disable 649
56 internal Type generic_type;
57 Type[] type_arguments;
59 #pragma warning restore 649
62 Hashtable fields, ctors, methods;
64 internal MonoGenericClass ()
66 // this should not be used
67 throw new InvalidOperationException ();
70 internal MonoGenericClass (Type tb, Type[] args)
72 this.generic_type = tb;
73 this.type_arguments = args;
75 This is a temporary hack until we can fix the rest of the runtime
76 to properly handle this class to be a complete UT.
78 We must not regisrer this with the runtime after the type is created
79 otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
80 which is very very broken.
82 if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
83 register_with_runtime (this);
87 internal override Type InternalResolve ()
89 Type gtd = generic_type.InternalResolve ();
90 Type[] args = new Type [type_arguments.Length];
91 for (int i = 0; i < type_arguments.Length; ++i)
92 args [i] = type_arguments [i].InternalResolve ();
93 return gtd.MakeGenericType (args);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 extern void initialize (FieldInfo[] fields);
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 internal static extern void register_with_runtime (Type type);
102 internal bool IsCreated {
104 TypeBuilder tb = generic_type as TypeBuilder;
105 return tb != null ? tb.is_created : true;
109 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
110 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
117 MonoGenericClass parent = GetParentType () as MonoGenericClass;
119 parent.initialize ();
121 initialize (generic_type.GetFields (flags));
126 Type GetParentType ()
128 return InflateType (generic_type.BaseType);
131 internal Type InflateType (Type type)
133 return InflateType (type, type_arguments, null);
136 internal Type InflateType (Type type, Type[] method_args)
138 return InflateType (type, type_arguments, method_args);
141 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
145 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
147 if (type.IsGenericParameter) {
148 if (type.DeclaringMethod == null)
149 return type_args == null ? type : type_args [type.GenericParameterPosition];
150 return method_args == null ? type : method_args [type.GenericParameterPosition];
153 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
155 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
157 if (type.GetArrayRank () > 1)
158 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
160 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
161 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
162 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
165 Type[] args = type.GetGenericArguments ();
166 for (int i = 0; i < args.Length; ++i)
167 args [i] = InflateType (args [i], type_args, method_args);
169 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
170 return gtd.MakeGenericType (args);
173 public override Type BaseType {
174 get { return generic_type.BaseType; }
177 public override Type[] GetInterfaces ()
179 throw new NotSupportedException ();
182 protected override bool IsValueTypeImpl ()
184 return generic_type.IsValueType;
187 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
192 methods = new Hashtable ();
193 if (!methods.ContainsKey (fromNoninstanciated))
194 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
195 return (MethodInfo)methods [fromNoninstanciated];
198 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
203 ctors = new Hashtable ();
204 if (!ctors.ContainsKey (fromNoninstanciated))
205 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
206 return (ConstructorInfo)ctors [fromNoninstanciated];
209 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
213 fields = new Hashtable ();
214 if (!fields.ContainsKey (fromNoninstanciated))
215 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
216 return (FieldInfo)fields [fromNoninstanciated];
219 public override MethodInfo[] GetMethods (BindingFlags bf)
221 throw new NotSupportedException ();
224 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
226 throw new NotSupportedException ();
229 public override FieldInfo[] GetFields (BindingFlags bf)
231 throw new NotSupportedException ();
234 public override PropertyInfo[] GetProperties (BindingFlags bf)
236 throw new NotSupportedException ();
239 public override EventInfo[] GetEvents (BindingFlags bf)
241 throw new NotSupportedException ();
244 public override Type[] GetNestedTypes (BindingFlags bf)
246 throw new NotSupportedException ();
249 public override bool IsAssignableFrom (Type c)
251 throw new NotSupportedException ();
254 public override Type UnderlyingSystemType {
258 public override Assembly Assembly {
259 get { return generic_type.Assembly; }
262 public override Module Module {
263 get { return generic_type.Module; }
266 public override string Name {
267 get { return generic_type.Name; }
270 public override string Namespace {
271 get { return generic_type.Namespace; }
274 public override string FullName {
275 get { return format_name (true, false); }
278 public override string AssemblyQualifiedName {
279 get { return format_name (true, true); }
282 public override Guid GUID {
283 get { throw new NotSupportedException (); }
286 string format_name (bool full_name, bool assembly_qualified)
288 StringBuilder sb = new StringBuilder (generic_type.FullName);
291 for (int i = 0; i < type_arguments.Length; ++i) {
297 string assemblyName = type_arguments [i].Assembly.FullName;
298 name = type_arguments [i].FullName;
299 if (name != null && assemblyName != null)
300 name = name + ", " + assemblyName;
302 name = type_arguments [i].ToString ();
314 if (assembly_qualified) {
316 sb.Append (generic_type.Assembly.FullName);
318 return sb.ToString ();
321 public override string ToString ()
323 return format_name (false, false);
326 public override Type GetGenericTypeDefinition ()
331 public override Type[] GetGenericArguments ()
333 Type[] ret = new Type [type_arguments.Length];
334 type_arguments.CopyTo (ret, 0);
338 public override bool ContainsGenericParameters {
340 foreach (Type t in type_arguments) {
341 if (t.ContainsGenericParameters)
348 public override bool IsGenericTypeDefinition {
349 get { return false; }
352 public override bool IsGenericType {
356 public override Type DeclaringType {
357 get { return generic_type.DeclaringType; }
360 public override RuntimeTypeHandle TypeHandle {
362 throw new NotSupportedException ();
366 public override Type MakeArrayType ()
368 return new ArrayType (this, 0);
371 public override Type MakeArrayType (int rank)
374 throw new IndexOutOfRangeException ();
375 return new ArrayType (this, rank);
378 public override Type MakeByRefType ()
380 return new ByRefType (this);
383 public override Type MakePointerType ()
385 return new PointerType (this);
388 public override Type GetElementType ()
390 throw new NotSupportedException ();
393 protected override bool HasElementTypeImpl ()
398 protected override bool IsCOMObjectImpl ()
403 protected override bool IsPrimitiveImpl ()
408 protected override bool IsArrayImpl ()
413 protected override bool IsByRefImpl ()
418 protected override bool IsPointerImpl ()
423 protected override TypeAttributes GetAttributeFlagsImpl ()
425 return generic_type.Attributes;
429 public override Type GetInterface (string name, bool ignoreCase)
431 throw new NotSupportedException ();
434 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
436 throw new NotSupportedException ();
439 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
441 throw new NotSupportedException ();
444 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
446 throw new NotSupportedException ();
449 public override Type GetNestedType (string name, BindingFlags bindingAttr)
451 throw new NotSupportedException ();
454 public override object InvokeMember (string name, BindingFlags invokeAttr,
455 Binder binder, object target, object[] args,
456 ParameterModifier[] modifiers,
457 CultureInfo culture, string[] namedParameters)
459 throw new NotSupportedException ();
462 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
463 CallingConventions callConvention, Type[] types,
464 ParameterModifier[] modifiers)
466 throw new NotSupportedException ();
469 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
470 Type returnType, Type[] types, ParameterModifier[] modifiers)
472 throw new NotSupportedException ();
475 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
477 CallingConventions callConvention,
479 ParameterModifier[] modifiers)
481 throw new NotSupportedException ();
485 public override bool IsDefined (Type attributeType, bool inherit)
487 throw new NotSupportedException ();
490 public override object [] GetCustomAttributes (bool inherit)
492 throw new NotSupportedException ();
495 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
497 throw new NotSupportedException ();
500 internal override bool IsUserType {
502 foreach (var t in type_arguments) {