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 // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.Serialization;
37 using System.Security;
42 internal class MonoType : Type, ISerializable
45 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46 private static extern void type_from_obj (MonoType type, Object obj);
49 internal MonoType (Object obj)
51 // this should not be used - lupus
52 type_from_obj (this, obj);
54 throw new NotImplementedException ();
57 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58 private static extern TypeAttributes get_attributes (Type type);
60 protected override TypeAttributes GetAttributeFlagsImpl ()
62 return get_attributes (this);
65 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
67 CallingConventions callConvention,
69 ParameterModifier[] modifiers)
71 if (bindingAttr == BindingFlags.Default)
72 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
74 ConstructorInfo[] methods = GetConstructors (bindingAttr);
75 ConstructorInfo found = null;
78 foreach (ConstructorInfo m in methods) {
79 // Under MS.NET, Standard|HasThis matches Standard...
80 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
89 throw new AmbiguousMatchException ();
90 return (ConstructorInfo) CheckMethodSecurity (found);
92 match = new MethodBase [count];
97 foreach (ConstructorInfo m in methods) {
98 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
104 binder = Binder.DefaultBinder;
105 return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
108 [MethodImplAttribute(MethodImplOptions.InternalCall)]
109 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
111 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
113 return GetConstructors_internal (bindingAttr, this);
116 [MethodImplAttribute(MethodImplOptions.InternalCall)]
117 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
119 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
122 throw new ArgumentNullException ("name");
124 return InternalGetEvent (name, bindingAttr);
127 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
130 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
132 return GetEvents_internal (bindingAttr, this);
135 [MethodImplAttribute(MethodImplOptions.InternalCall)]
136 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
138 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
141 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
143 return GetFields_internal (bindingAttr, this);
146 public override Type GetInterface (string name, bool ignoreCase)
149 throw new ArgumentNullException ();
151 Type[] interfaces = GetInterfaces();
153 foreach (Type type in interfaces) {
154 if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
156 if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
163 [MethodImplAttribute(MethodImplOptions.InternalCall)]
164 public extern override Type[] GetInterfaces();
166 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
168 return FindMembers (MemberTypes.All, bindingAttr, null, null);
171 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
174 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
176 return GetMethodsByName (null, bindingAttr, false, this);
179 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
181 CallingConventions callConvention,
182 Type[] types, ParameterModifier[] modifiers)
184 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
185 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
186 MethodInfo found = null;
188 int typesLen = (types != null) ? types.Length : 0;
191 foreach (MethodInfo m in methods) {
192 // Under MS.NET, Standard|HasThis matches Standard...
193 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
202 if (count == 1 && typesLen == 0)
203 return (MethodInfo) CheckMethodSecurity (found);
205 match = new MethodBase [count];
210 foreach (MethodInfo m in methods) {
211 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
218 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
221 binder = Binder.DefaultBinder;
223 return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
226 [MethodImplAttribute(MethodImplOptions.InternalCall)]
227 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
229 [MethodImplAttribute(MethodImplOptions.InternalCall)]
230 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
232 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
234 return GetCorrespondingInflatedMethod (fromNoninstanciated);
237 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
239 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
242 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
244 return GetField (fromNoninstanciated.Name);
247 [MethodImplAttribute(MethodImplOptions.InternalCall)]
248 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
250 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
253 [MethodImplAttribute(MethodImplOptions.InternalCall)]
254 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
256 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
258 return GetPropertiesByName (null, bindingAttr, false, this);
261 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
262 Binder binder, Type returnType,
264 ParameterModifier[] modifiers)
266 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
267 PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
268 int count = props.Length;
272 if (count == 1 && (types == null || types.Length == 0))
276 binder = Binder.DefaultBinder;
278 return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
281 protected override bool HasElementTypeImpl ()
283 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
286 protected override bool IsArrayImpl ()
288 return Type.IsArrayImpl (this);
291 [MethodImplAttribute(MethodImplOptions.InternalCall)]
292 protected extern override bool IsByRefImpl ();
294 protected override bool IsCOMObjectImpl ()
299 [MethodImplAttribute(MethodImplOptions.InternalCall)]
300 protected extern override bool IsPointerImpl ();
302 [MethodImplAttribute(MethodImplOptions.InternalCall)]
303 protected extern override bool IsPrimitiveImpl ();
305 protected override bool IsValueTypeImpl ()
307 return type_is_subtype_of (this, typeof (System.ValueType), false) &&
308 this != typeof (System.ValueType) &&
309 this != typeof (System.Enum);
312 public override object InvokeMember (string name, BindingFlags invokeAttr,
313 Binder binder, object target, object[] args,
314 ParameterModifier[] modifiers,
315 CultureInfo culture, string[] namedParameters)
318 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
319 if ((invokeAttr & (BindingFlags.GetField |
320 BindingFlags.GetField | BindingFlags.GetProperty |
321 BindingFlags.SetProperty)) != 0)
322 throw new ArgumentException ("invokeAttr");
323 } else if (name == null)
324 throw new ArgumentNullException ("name");
325 if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
326 throw new ArgumentException ("invokeAttr");
327 if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
328 throw new ArgumentException ("invokeAttr");
329 if ((invokeAttr & BindingFlags.InvokeMethod) != 0 && (invokeAttr & (BindingFlags.SetProperty|BindingFlags.SetField)) != 0)
330 throw new ArgumentException ("invokeAttr");
331 if ((invokeAttr & BindingFlags.SetField) != 0 && ((args == null) || args.Length != 1))
332 throw new ArgumentException ("invokeAttr");
333 if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
334 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
336 /* set some defaults if none are provided :-( */
337 if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
338 invokeAttr |= BindingFlags.Public;
339 if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
340 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
343 binder = Binder.DefaultBinder;
344 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
345 /* the name is ignored */
346 invokeAttr |= BindingFlags.DeclaredOnly;
347 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
349 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
351 if (this.IsValueType && args == null)
352 return Activator.CreateInstanceInternal (this);
354 throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
356 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
357 binder.ReorderArgumentArray (ref args, state);
360 if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
361 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
362 name = attr.MemberName;
364 bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
365 string throwMissingMethodDescription = null;
366 bool throwMissingFieldException = false;
368 if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
369 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
371 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
373 if (methods.Length > 0)
374 throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter.";
376 throwMissingMethodDescription = "Cannot find method " + name + ".";
378 object result = m.Invoke (target, invokeAttr, binder, args, culture);
379 binder.ReorderArgumentArray (ref args, state);
383 if ((invokeAttr & BindingFlags.GetField) != 0) {
384 FieldInfo f = GetField (name, invokeAttr);
386 return f.GetValue (target);
387 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
388 throwMissingFieldException = true;
390 /* try GetProperty */
391 } else if ((invokeAttr & BindingFlags.SetField) != 0) {
392 FieldInfo f = GetField (name, invokeAttr);
394 f.SetValue (target, args [0]);
396 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
397 throwMissingFieldException = true;
399 /* try SetProperty */
401 if ((invokeAttr & BindingFlags.GetProperty) != 0) {
402 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
405 for (i = 0; i < properties.Length; ++i) {
406 if ((properties [i].GetGetMethod (true) != null))
409 MethodBase[] smethods = new MethodBase [count];
411 for (i = 0; i < properties.Length; ++i) {
412 MethodBase mb = properties [i].GetGetMethod (true);
414 smethods [count++] = mb;
416 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
418 throwMissingFieldException = true;
420 object result = m.Invoke (target, invokeAttr, binder, args, culture);
421 binder.ReorderArgumentArray (ref args, state);
424 } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
425 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
428 for (i = 0; i < properties.Length; ++i) {
429 if (properties [i].GetSetMethod (true) != null)
432 MethodBase[] smethods = new MethodBase [count];
434 for (i = 0; i < properties.Length; ++i) {
435 MethodBase mb = properties [i].GetSetMethod (true);
437 smethods [count++] = mb;
439 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
441 throwMissingFieldException = true;
443 object result = m.Invoke (target, invokeAttr, binder, args, culture);
444 binder.ReorderArgumentArray (ref args, state);
448 if (throwMissingMethodDescription != null)
449 throw new MissingMethodException(throwMissingMethodDescription);
450 if (throwMissingFieldException)
451 throw new MissingFieldException("Cannot find variable " + name + ".");
456 [MethodImplAttribute(MethodImplOptions.InternalCall)]
457 public extern override Type GetElementType ();
459 public override Type UnderlyingSystemType {
461 // This has _nothing_ to do with getting the base type of an enum etc.
466 public extern override Assembly Assembly {
467 [MethodImplAttribute(MethodImplOptions.InternalCall)]
471 public override string AssemblyQualifiedName {
473 return getFullName (true, true);
477 [MethodImplAttribute(MethodImplOptions.InternalCall)]
478 private extern string getFullName(bool full_name, bool assembly_qualified);
480 public extern override Type BaseType {
481 [MethodImplAttribute(MethodImplOptions.InternalCall)]
485 public override string FullName {
487 return getFullName (true, false);
491 public override Guid GUID {
493 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
496 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
500 public override bool IsDefined (Type attributeType, bool inherit)
502 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
505 public override object[] GetCustomAttributes (bool inherit)
507 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
510 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
512 if (attributeType == null)
514 throw new ArgumentNullException("attributeType");
517 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
520 public override MemberTypes MemberType {
522 if (DeclaringType != null)
523 return MemberTypes.NestedType;
525 return MemberTypes.TypeInfo;
529 public extern override string Name {
530 [MethodImplAttribute(MethodImplOptions.InternalCall)]
534 public extern override string Namespace {
535 [MethodImplAttribute(MethodImplOptions.InternalCall)]
539 public extern override Module Module {
540 [MethodImplAttribute(MethodImplOptions.InternalCall)]
544 public extern override Type DeclaringType {
545 [MethodImplAttribute(MethodImplOptions.InternalCall)]
549 public override Type ReflectedType {
551 return DeclaringType;
555 public override RuntimeTypeHandle TypeHandle {
561 [MethodImplAttribute(MethodImplOptions.InternalCall)]
562 public extern override int GetArrayRank ();
564 public void GetObjectData(SerializationInfo info, StreamingContext context)
566 UnitySerializationHolder.GetTypeData (this, info, context);
569 public override string ToString()
571 return getFullName (false, false);
574 #if NET_2_0 || BOOTSTRAP_NET_2_0
575 [MethodImplAttribute(MethodImplOptions.InternalCall)]
576 public extern override Type [] GetGenericArguments ();
578 public override bool ContainsGenericParameters {
580 if (IsGenericParameter)
584 foreach (Type arg in GetGenericArguments ())
585 if (arg.ContainsGenericParameters)
590 return GetElementType ().ContainsGenericParameters;
596 public extern override bool IsGenericParameter {
597 [MethodImplAttribute(MethodImplOptions.InternalCall)]
601 public extern override MethodBase DeclaringMethod {
602 [MethodImplAttribute(MethodImplOptions.InternalCall)]
607 private MethodBase CheckMethodSecurity (MethodBase mb)
609 if (!SecurityManager.SecurityEnabled || (mb == null))
612 // Sadly we have no way to know which kind of security action this is
613 // so we must do it the hard way. Actually this isn't so bad
614 // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
615 // icall required (and do it ourselves)
617 // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
618 return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;