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 internal class MonoGenericClass : Type
55 #region Keep in sync with object-internals.h
56 #pragma warning disable 649
57 internal Type generic_type;
58 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;
76 This is a temporary hack until we can fix the rest of the runtime
77 to properly handle this class to be a complete UT.
79 We must not regisrer this with the runtime after the type is created
80 otherwise created_type.MakeGenericType will return an instance of MonoGenericClass,
81 which is very very broken.
83 if (tb is TypeBuilder && !(tb as TypeBuilder).is_created)
84 register_with_runtime (this);
88 internal override Type InternalResolve ()
90 Type gtd = generic_type.InternalResolve ();
91 Type[] args = new Type [type_arguments.Length];
92 for (int i = 0; i < type_arguments.Length; ++i)
93 args [i] = type_arguments [i].InternalResolve ();
94 return gtd.MakeGenericType (args);
97 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98 extern void initialize (FieldInfo[] fields);
100 [MethodImplAttribute(MethodImplOptions.InternalCall)]
101 internal static extern void register_with_runtime (Type type);
103 internal bool IsCreated {
105 TypeBuilder tb = generic_type as TypeBuilder;
106 return tb != null ? tb.is_created : true;
110 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
111 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
118 MonoGenericClass parent = GetParentType () as MonoGenericClass;
120 parent.initialize ();
122 initialize (generic_type.GetFields (flags));
127 Type GetParentType ()
129 return InflateType (generic_type.BaseType);
132 internal Type InflateType (Type type)
134 return InflateType (type, type_arguments, null);
137 internal Type InflateType (Type type, Type[] method_args)
139 return InflateType (type, type_arguments, method_args);
142 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
146 if (!type.IsGenericParameter && !type.ContainsGenericParameters)
148 if (type.IsGenericParameter) {
149 if (type.DeclaringMethod == null)
150 return type_args == null ? type : type_args [type.GenericParameterPosition];
151 return method_args == null ? type : method_args [type.GenericParameterPosition];
154 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
156 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
158 if (type.GetArrayRank () > 1)
159 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
161 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
162 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
163 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
166 Type[] args = type.GetGenericArguments ();
167 for (int i = 0; i < args.Length; ++i)
168 args [i] = InflateType (args [i], type_args, method_args);
170 Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
171 return gtd.MakeGenericType (args);
174 public override Type BaseType {
175 get { return generic_type.BaseType; }
178 public override Type[] GetInterfaces ()
180 throw new NotSupportedException ();
183 protected override bool IsValueTypeImpl ()
185 return generic_type.IsValueType;
188 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
193 methods = new Hashtable ();
194 if (!methods.ContainsKey (fromNoninstanciated))
195 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
196 return (MethodInfo)methods [fromNoninstanciated];
199 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
204 ctors = new Hashtable ();
205 if (!ctors.ContainsKey (fromNoninstanciated))
206 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
207 return (ConstructorInfo)ctors [fromNoninstanciated];
210 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
214 fields = new Hashtable ();
215 if (!fields.ContainsKey (fromNoninstanciated))
216 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
217 return (FieldInfo)fields [fromNoninstanciated];
220 public override MethodInfo[] GetMethods (BindingFlags bf)
222 throw new NotSupportedException ();
225 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
227 throw new NotSupportedException ();
230 public override FieldInfo[] GetFields (BindingFlags bf)
232 throw new NotSupportedException ();
235 public override PropertyInfo[] GetProperties (BindingFlags bf)
237 throw new NotSupportedException ();
240 public override EventInfo[] GetEvents (BindingFlags bf)
242 throw new NotSupportedException ();
245 public override Type[] GetNestedTypes (BindingFlags bf)
247 throw new NotSupportedException ();
250 public override bool IsAssignableFrom (Type c)
252 throw new NotSupportedException ();
255 public override Type UnderlyingSystemType {
259 public override Assembly Assembly {
260 get { return generic_type.Assembly; }
263 public override Module Module {
264 get { return generic_type.Module; }
267 public override string Name {
268 get { return generic_type.Name; }
271 public override string Namespace {
272 get { return generic_type.Namespace; }
275 public override string FullName {
276 get { return format_name (true, false); }
279 public override string AssemblyQualifiedName {
280 get { return format_name (true, true); }
283 public override Guid GUID {
284 get { throw new NotSupportedException (); }
287 string format_name (bool full_name, bool assembly_qualified)
289 StringBuilder sb = new StringBuilder (generic_type.FullName);
292 for (int i = 0; i < type_arguments.Length; ++i) {
298 string assemblyName = type_arguments [i].Assembly.FullName;
299 name = type_arguments [i].FullName;
300 if (name != null && assemblyName != null)
301 name = name + ", " + assemblyName;
303 name = type_arguments [i].ToString ();
315 if (assembly_qualified) {
317 sb.Append (generic_type.Assembly.FullName);
319 return sb.ToString ();
322 public override string ToString ()
324 return format_name (false, false);
327 public override Type GetGenericTypeDefinition ()
332 public override Type[] GetGenericArguments ()
334 Type[] ret = new Type [type_arguments.Length];
335 type_arguments.CopyTo (ret, 0);
339 public override bool ContainsGenericParameters {
341 foreach (Type t in type_arguments) {
342 if (t.ContainsGenericParameters)
349 public override bool IsGenericTypeDefinition {
350 get { return false; }
353 public override bool IsGenericType {
357 public override Type DeclaringType {
358 get { return generic_type.DeclaringType; }
361 public override RuntimeTypeHandle TypeHandle {
363 throw new NotSupportedException ();
367 public override Type MakeArrayType ()
369 return new ArrayType (this, 0);
372 public override Type MakeArrayType (int rank)
375 throw new IndexOutOfRangeException ();
376 return new ArrayType (this, rank);
379 public override Type MakeByRefType ()
381 return new ByRefType (this);
384 public override Type MakePointerType ()
386 return new PointerType (this);
389 public override Type GetElementType ()
391 throw new NotSupportedException ();
394 protected override bool HasElementTypeImpl ()
399 protected override bool IsCOMObjectImpl ()
404 protected override bool IsPrimitiveImpl ()
409 protected override bool IsArrayImpl ()
414 protected override bool IsByRefImpl ()
419 protected override bool IsPointerImpl ()
424 protected override TypeAttributes GetAttributeFlagsImpl ()
426 return generic_type.Attributes;
430 public override Type GetInterface (string name, bool ignoreCase)
432 throw new NotSupportedException ();
435 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
437 throw new NotSupportedException ();
440 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
442 throw new NotSupportedException ();
445 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
447 throw new NotSupportedException ();
450 public override Type GetNestedType (string name, BindingFlags bindingAttr)
452 throw new NotSupportedException ();
455 public override object InvokeMember (string name, BindingFlags invokeAttr,
456 Binder binder, object target, object[] args,
457 ParameterModifier[] modifiers,
458 CultureInfo culture, string[] namedParameters)
460 throw new NotSupportedException ();
463 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
464 CallingConventions callConvention, Type[] types,
465 ParameterModifier[] modifiers)
467 throw new NotSupportedException ();
470 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
471 Type returnType, Type[] types, ParameterModifier[] modifiers)
473 throw new NotSupportedException ();
476 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
478 CallingConventions callConvention,
480 ParameterModifier[] modifiers)
482 throw new NotSupportedException ();
486 public override bool IsDefined (Type attributeType, bool inherit)
488 throw new NotSupportedException ();
491 public override object [] GetCustomAttributes (bool inherit)
493 throw new NotSupportedException ();
496 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
498 throw new NotSupportedException ();
501 internal override bool IsUserType {
503 foreach (var t in type_arguments) {