5 // Sean MacIsaac (macisaac@ximian.com)
6 // Paolo Molaro (lupus@ximian.com)
7 // Patrik Torstensson (patrik.torstensson@labs2.com)
8 // Gonzalo Paniagua (gonzalo@ximian.com)
10 // (c) 2001-2003 Ximian, Inc.
11 // (c) 2003,2004 Novell, Inc. (http://www.novell.com)
14 using System.Reflection;
15 using System.Runtime.CompilerServices;
16 using System.Globalization;
17 using System.Runtime.Serialization;
22 internal class MonoType : Type, ISerializable
25 [MethodImplAttribute(MethodImplOptions.InternalCall)]
26 private static extern void type_from_obj (MonoType type, Object obj);
29 internal MonoType (Object obj)
31 // this should not be used - lupus
32 type_from_obj (this, obj);
34 throw new NotImplementedException ();
37 [MethodImplAttribute(MethodImplOptions.InternalCall)]
38 private static extern TypeAttributes get_attributes (Type type);
40 protected override TypeAttributes GetAttributeFlagsImpl ()
42 return get_attributes (this);
45 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
47 CallingConventions callConvention,
49 ParameterModifier[] modifiers)
51 if (bindingAttr == BindingFlags.Default)
52 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
54 ConstructorInfo[] methods = GetConstructors (bindingAttr);
55 ConstructorInfo found = null;
58 foreach (ConstructorInfo m in methods) {
59 // Under MS.NET, Standard|HasThis matches Standard...
60 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
69 throw new AmbiguousMatchException ();
72 match = new MethodBase [count];
77 foreach (ConstructorInfo m in methods) {
78 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
84 binder = Binder.DefaultBinder;
85 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
88 [MethodImplAttribute(MethodImplOptions.InternalCall)]
89 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
91 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
93 return GetConstructors_internal (bindingAttr, this);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
99 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
102 throw new ArgumentNullException ("name");
104 return InternalGetEvent (name, bindingAttr);
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
110 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
112 return GetEvents_internal (bindingAttr, this);
115 [MethodImplAttribute(MethodImplOptions.InternalCall)]
116 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
118 [MethodImplAttribute(MethodImplOptions.InternalCall)]
119 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
121 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
123 return GetFields_internal (bindingAttr, this);
126 public override Type GetInterface (string name, bool ignoreCase)
129 throw new ArgumentNullException ();
131 Type[] interfaces = GetInterfaces();
133 foreach (Type type in interfaces) {
134 if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
136 if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
143 [MethodImplAttribute(MethodImplOptions.InternalCall)]
144 public extern override Type[] GetInterfaces();
146 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
148 return FindMembers (MemberTypes.All, bindingAttr, null, null);
151 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
154 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
156 return GetMethodsByName (null, bindingAttr, false, this);
159 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
161 CallingConventions callConvention,
162 Type[] types, ParameterModifier[] modifiers)
164 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
165 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
166 MethodInfo found = null;
168 int typesLen = (types != null) ? types.Length : 0;
171 foreach (MethodInfo m in methods) {
172 // Under MS.NET, Standard|HasThis matches Standard...
173 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
182 if (count == 1 && typesLen == 0)
185 match = new MethodBase [count];
190 foreach (MethodInfo m in methods) {
191 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
198 return (MethodInfo) Binder.FindMostDerivedMatch (match);
201 binder = Binder.DefaultBinder;
203 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
206 [MethodImplAttribute(MethodImplOptions.InternalCall)]
207 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
209 [MethodImplAttribute(MethodImplOptions.InternalCall)]
210 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
212 [MethodImplAttribute(MethodImplOptions.InternalCall)]
213 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
215 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
217 return GetPropertiesByName (null, bindingAttr, false, this);
220 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
221 Binder binder, Type returnType,
223 ParameterModifier[] modifiers)
225 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
226 PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
227 int count = props.Length;
231 if (count == 1 && (types == null || types.Length == 0))
235 binder = Binder.DefaultBinder;
237 return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
240 protected override bool HasElementTypeImpl ()
242 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
245 protected override bool IsArrayImpl ()
247 return Type.IsArrayImpl (this);
250 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251 protected extern override bool IsByRefImpl ();
253 protected override bool IsCOMObjectImpl ()
258 [MethodImplAttribute(MethodImplOptions.InternalCall)]
259 protected extern override bool IsPointerImpl ();
261 [MethodImplAttribute(MethodImplOptions.InternalCall)]
262 protected extern override bool IsPrimitiveImpl ();
264 protected override bool IsValueTypeImpl ()
266 return type_is_subtype_of (this, typeof (System.ValueType), false) &&
267 this != typeof (System.ValueType) &&
268 this != typeof (System.Enum);
271 public override object InvokeMember (string name, BindingFlags invokeAttr,
272 Binder binder, object target, object[] args,
273 ParameterModifier[] modifiers,
274 CultureInfo culture, string[] namedParameters)
277 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
278 if ((invokeAttr & (BindingFlags.GetField |
279 BindingFlags.GetField | BindingFlags.GetProperty |
280 BindingFlags.SetProperty)) != 0)
281 throw new ArgumentException ("invokeAttr");
282 } else if (name == null)
283 throw new ArgumentNullException ("name");
284 if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
285 throw new ArgumentException ("invokeAttr");
286 if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
287 throw new ArgumentException ("invokeAttr");
288 if ((invokeAttr & BindingFlags.InvokeMethod) != 0 && (invokeAttr & (BindingFlags.SetProperty|BindingFlags.SetField)) != 0)
289 throw new ArgumentException ("invokeAttr");
290 if ((invokeAttr & BindingFlags.SetField) != 0 && ((args == null) || args.Length != 1))
291 throw new ArgumentException ("invokeAttr");
292 if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
293 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
295 /* set some defaults if none are provided :-( */
296 if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
297 invokeAttr |= BindingFlags.Public;
298 if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
299 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
302 binder = Binder.DefaultBinder;
303 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
304 /* the name is ignored */
305 invokeAttr |= BindingFlags.DeclaredOnly;
306 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
308 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
310 throw new MissingMethodException ();
311 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
312 binder.ReorderArgumentArray (ref args, state);
315 if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
316 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
317 name = attr.MemberName;
319 bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
320 if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
321 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
323 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
325 throw new MissingMethodException ();
326 object result = m.Invoke (target, invokeAttr, binder, args, culture);
327 binder.ReorderArgumentArray (ref args, state);
330 if ((invokeAttr & BindingFlags.GetField) != 0) {
331 FieldInfo f = GetField (name, invokeAttr);
333 return f.GetValue (target);
334 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
335 throw new MissingFieldException ();
337 /* try GetProperty */
338 } else if ((invokeAttr & BindingFlags.SetField) != 0) {
339 FieldInfo f = GetField (name, invokeAttr);
341 f.SetValue (target, args [0]);
343 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
344 throw new MissingFieldException ();
346 /* try SetProperty */
348 if ((invokeAttr & BindingFlags.GetProperty) != 0) {
349 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
352 for (i = 0; i < properties.Length; ++i) {
353 if ((properties [i].GetGetMethod () != null))
356 MethodBase[] smethods = new MethodBase [count];
358 for (i = 0; i < properties.Length; ++i) {
359 MethodBase mb = properties [i].GetGetMethod ();
361 smethods [count++] = mb;
363 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
365 throw new MissingFieldException ();
366 object result = m.Invoke (target, invokeAttr, binder, args, culture);
367 binder.ReorderArgumentArray (ref args, state);
369 } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
370 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
373 for (i = 0; i < properties.Length; ++i) {
374 if (properties [i].GetSetMethod () != null)
377 MethodBase[] smethods = new MethodBase [count];
379 for (i = 0; i < properties.Length; ++i) {
380 MethodBase mb = properties [i].GetSetMethod ();
382 smethods [count++] = mb;
384 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
386 throw new MissingFieldException ();
387 object result = m.Invoke (target, invokeAttr, binder, args, culture);
388 binder.ReorderArgumentArray (ref args, state);
394 [MethodImplAttribute(MethodImplOptions.InternalCall)]
395 public extern override Type GetElementType ();
397 public extern override Type UnderlyingSystemType {
398 [MethodImplAttribute(MethodImplOptions.InternalCall)]
402 public extern override Assembly Assembly {
403 [MethodImplAttribute(MethodImplOptions.InternalCall)]
407 public override string AssemblyQualifiedName {
409 return getFullName () + ", " + Assembly.GetName ().ToString ();
413 [MethodImplAttribute(MethodImplOptions.InternalCall)]
414 private extern string getFullName();
416 public extern override Type BaseType {
417 [MethodImplAttribute(MethodImplOptions.InternalCall)]
421 public override string FullName {
423 return getFullName ();
427 public override Guid GUID {
433 public override bool IsDefined (Type attributeType, bool inherit)
435 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
438 public override object[] GetCustomAttributes (bool inherit)
440 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
443 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
445 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
448 public override MemberTypes MemberType {
450 if (DeclaringType != null)
451 return MemberTypes.NestedType;
453 return MemberTypes.TypeInfo;
457 public extern override string Name {
458 [MethodImplAttribute(MethodImplOptions.InternalCall)]
462 public extern override string Namespace {
463 [MethodImplAttribute(MethodImplOptions.InternalCall)]
467 public extern override Module Module {
468 [MethodImplAttribute(MethodImplOptions.InternalCall)]
472 public extern override Type DeclaringType {
473 [MethodImplAttribute(MethodImplOptions.InternalCall)]
477 public override Type ReflectedType {
479 return DeclaringType;
483 public override RuntimeTypeHandle TypeHandle {
489 [MethodImplAttribute(MethodImplOptions.InternalCall)]
490 public extern override int GetArrayRank ();
492 public void GetObjectData(SerializationInfo info, StreamingContext context)
494 UnitySerializationHolder.GetTypeData (this, info, context);
497 #if NET_2_0 || BOOTSTRAP_NET_1_2
498 [MethodImplAttribute(MethodImplOptions.InternalCall)]
499 public extern override Type [] GetGenericArguments ();
501 public extern override bool HasGenericArguments {
502 [MethodImplAttribute(MethodImplOptions.InternalCall)]
506 public override bool ContainsGenericParameters {
508 if (IsGenericParameter)
511 if (HasGenericArguments) {
512 foreach (Type arg in GetGenericArguments ())
513 if (arg.ContainsGenericParameters)
518 return GetElementType ().ContainsGenericParameters;
524 public extern override bool IsGenericParameter {
525 [MethodImplAttribute(MethodImplOptions.InternalCall)]
529 public extern override MethodInfo DeclaringMethod {
530 [MethodImplAttribute(MethodImplOptions.InternalCall)]