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;
61 #pragma warning restore 649
64 Hashtable fields, ctors, methods;
66 internal MonoGenericClass ()
68 // this should not be used
69 throw new InvalidOperationException ();
72 internal MonoGenericClass (Type tb, Type[] args)
74 this.generic_type = tb;
75 this.type_arguments = args;
77 This is a temporary hack until we can fix the rest of the runtime
78 to properly handle this class to be a complete UT.
80 We must not regisrer this with the runtime after the type is created
81 otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
82 which is very very broken.
84 if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
85 register_with_runtime (this);
89 internal override Type InternalResolve ()
91 Type gtd = generic_type.InternalResolve ();
92 Type[] args = new Type [type_arguments.Length];
93 for (int i = 0; i < type_arguments.Length; ++i)
94 args [i] = type_arguments [i].InternalResolve ();
95 return gtd.MakeGenericType (args);
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 extern void initialize (FieldInfo[] fields);
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 internal static extern void register_with_runtime (Type type);
104 internal bool IsCreated {
106 TypeBuilder tb = generic_type as TypeBuilder;
107 return tb != null ? tb.is_created : true;
111 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
112 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
119 MonoGenericClass parent = GetParentType () as MonoGenericClass;
121 parent.initialize ();
123 initialize (generic_type.GetFields (flags));
128 Type GetParentType ()
130 return InflateType (generic_type.BaseType);
133 internal Type InflateType (Type type)
135 return InflateType (type, type_arguments, null);
138 internal Type InflateType (Type type, Type[] method_args)
140 return InflateType (type, type_arguments, method_args);
143 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
147 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
149 if (type.IsGenericParameter) {
150 if (type.DeclaringMethod == null)
151 return type_args == null ? type : type_args [type.GenericParameterPosition];
152 return method_args == null ? type : method_args [type.GenericParameterPosition];
155 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
157 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
159 if (type.GetArrayRank () > 1)
160 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
162 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
163 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
164 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
167 Type[] args = type.GetGenericArguments ();
168 for (int i = 0; i < args.Length; ++i)
169 args [i] = InflateType (args [i], type_args, method_args);
171 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
172 return gtd.MakeGenericType (args);
175 public override Type BaseType {
176 get { return generic_type.BaseType; }
179 public override Type[] GetInterfaces ()
181 throw new NotSupportedException ();
184 protected override bool IsValueTypeImpl ()
186 return generic_type.IsValueType;
189 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
194 methods = new Hashtable ();
195 if (!methods.ContainsKey (fromNoninstanciated))
196 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
197 return (MethodInfo)methods [fromNoninstanciated];
200 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
205 ctors = new Hashtable ();
206 if (!ctors.ContainsKey (fromNoninstanciated))
207 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
208 return (ConstructorInfo)ctors [fromNoninstanciated];
211 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
215 fields = new Hashtable ();
216 if (!fields.ContainsKey (fromNoninstanciated))
217 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
218 return (FieldInfo)fields [fromNoninstanciated];
221 public override MethodInfo[] GetMethods (BindingFlags bf)
223 throw new NotSupportedException ();
226 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
228 throw new NotSupportedException ();
231 public override FieldInfo[] GetFields (BindingFlags bf)
233 throw new NotSupportedException ();
236 public override PropertyInfo[] GetProperties (BindingFlags bf)
238 throw new NotSupportedException ();
241 public override EventInfo[] GetEvents (BindingFlags bf)
243 throw new NotSupportedException ();
246 public override Type[] GetNestedTypes (BindingFlags bf)
248 throw new NotSupportedException ();
251 public override bool IsAssignableFrom (Type c)
253 throw new NotSupportedException ();
256 public override Type UnderlyingSystemType {
260 public override Assembly Assembly {
261 get { return generic_type.Assembly; }
264 public override Module Module {
265 get { return generic_type.Module; }
268 public override string Name {
269 get { return generic_type.Name; }
272 public override string Namespace {
273 get { return generic_type.Namespace; }
276 public override string FullName {
277 get { return format_name (true, false); }
280 public override string AssemblyQualifiedName {
281 get { return format_name (true, true); }
284 public override Guid GUID {
285 get { throw new NotSupportedException (); }
288 string format_name (bool full_name, bool assembly_qualified)
290 StringBuilder sb = new StringBuilder (generic_type.FullName);
293 for (int i = 0; i < type_arguments.Length; ++i) {
299 string assemblyName = type_arguments [i].Assembly.FullName;
300 name = type_arguments [i].FullName;
301 if (name != null && assemblyName != null)
302 name = name + ", " + assemblyName;
304 name = type_arguments [i].ToString ();
316 if (assembly_qualified) {
318 sb.Append (generic_type.Assembly.FullName);
320 return sb.ToString ();
323 public override string ToString ()
325 return format_name (false, false);
328 public override Type GetGenericTypeDefinition ()
333 public override Type[] GetGenericArguments ()
335 Type[] ret = new Type [type_arguments.Length];
336 type_arguments.CopyTo (ret, 0);
340 public override bool ContainsGenericParameters {
342 foreach (Type t in type_arguments) {
343 if (t.ContainsGenericParameters)
350 public override bool IsGenericTypeDefinition {
351 get { return false; }
354 public override bool IsGenericType {
358 public override Type DeclaringType {
359 get { return generic_type.DeclaringType; }
362 public override RuntimeTypeHandle TypeHandle {
364 throw new NotSupportedException ();
368 public override Type MakeArrayType ()
370 return new ArrayType (this, 0);
373 public override Type MakeArrayType (int rank)
376 throw new IndexOutOfRangeException ();
377 return new ArrayType (this, rank);
380 public override Type MakeByRefType ()
382 return new ByRefType (this);
385 public override Type MakePointerType ()
387 return new PointerType (this);
390 public override Type GetElementType ()
392 throw new NotSupportedException ();
395 protected override bool HasElementTypeImpl ()
400 protected override bool IsCOMObjectImpl ()
405 protected override bool IsPrimitiveImpl ()
410 protected override bool IsArrayImpl ()
415 protected override bool IsByRefImpl ()
420 protected override bool IsPointerImpl ()
425 protected override TypeAttributes GetAttributeFlagsImpl ()
427 return generic_type.Attributes;
431 public override Type GetInterface (string name, bool ignoreCase)
433 throw new NotSupportedException ();
436 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
438 throw new NotSupportedException ();
441 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
443 throw new NotSupportedException ();
446 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
448 throw new NotSupportedException ();
451 public override Type GetNestedType (string name, BindingFlags bindingAttr)
453 throw new NotSupportedException ();
456 public override object InvokeMember (string name, BindingFlags invokeAttr,
457 Binder binder, object target, object[] args,
458 ParameterModifier[] modifiers,
459 CultureInfo culture, string[] namedParameters)
461 throw new NotSupportedException ();
464 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
465 CallingConventions callConvention, Type[] types,
466 ParameterModifier[] modifiers)
468 throw new NotSupportedException ();
471 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
472 Type returnType, Type[] types, ParameterModifier[] modifiers)
474 throw new NotSupportedException ();
477 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
479 CallingConventions callConvention,
481 ParameterModifier[] modifiers)
483 throw new NotSupportedException ();
487 public override bool IsDefined (Type attributeType, bool inherit)
489 throw new NotSupportedException ();
492 public override object [] GetCustomAttributes (bool inherit)
495 return generic_type.GetCustomAttributes (inherit);
496 throw new NotSupportedException ();
499 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
502 return generic_type.GetCustomAttributes (attributeType, inherit);
503 throw new NotSupportedException ();
506 internal override bool IsUserType {
508 foreach (var t in type_arguments) {