New tests.
[mono.git] / mcs / class / corlib / System / MonoType.cs
index 8b3e0223eb8b95ae1dbe43dd6adf0dcc70620bd9..fe3bca9630b553fcffe19b47b5b6b7d64d126d1b 100644 (file)
@@ -30,6 +30,7 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System.Collections.Generic;
 using System.Globalization;
 using System.Reflection;
 using System.Runtime.CompilerServices;
@@ -70,9 +71,9 @@ namespace System
                        if (type_info == null)
                                type_info = new MonoTypeInfo ();
                        if ((ctor = type_info.default_ctor) == null) {
-                               BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
+                               const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
        
-                                ctor = type_info.default_ctor = GetConstructor (flags,  null, CallingConventions.Any, Type.EmptyTypes, null);
+                               ctor = type_info.default_ctor = GetConstructor (flags,  null, CallingConventions.Any, Type.EmptyTypes, null);
                        }
 
                        return ctor;
@@ -88,11 +89,20 @@ namespace System
                                                                       CallingConventions callConvention,
                                                                       Type[] types,
                                                                       ParameterModifier[] modifiers)
+               {
+                       ConstructorInfo[] methods = GetConstructors (bindingAttr);
+                       return GetConstructorImpl (methods, bindingAttr, binder, callConvention, types, modifiers);
+               }
+
+               internal static ConstructorInfo GetConstructorImpl (ConstructorInfo[] methods, BindingFlags bindingAttr,
+                                                                      Binder binder,
+                                                                      CallingConventions callConvention,
+                                                                      Type[] types,
+                                                                      ParameterModifier[] modifiers)
                {
                        if (bindingAttr == BindingFlags.Default)
                                bindingAttr = BindingFlags.Public | BindingFlags.Instance;
 
-                       ConstructorInfo[] methods = GetConstructors (bindingAttr);
                        ConstructorInfo found = null;
                        MethodBase[] match;
                        int count = 0;
@@ -172,9 +182,12 @@ namespace System
                        Type[] interfaces = GetInterfaces();
 
                        foreach (Type type in interfaces) {
-                               if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
+                               /*We must compare against the generic type definition*/
+                               Type t = type.IsGenericType ? type.GetGenericTypeDefinition () : type;
+
+                               if (String.Compare (t.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
                                        return type;
-                               if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
+                               if (String.Compare (t.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
                                        return type;
                        }
 
@@ -251,11 +264,15 @@ namespace System
 
                internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
                 {
+                       if (fromNoninstanciated == null)
+                               throw new ArgumentNullException ("fromNoninstanciated");
                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
                 }
 
                internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
                {
+                       if (fromNoninstanciated == null)
+                               throw new ArgumentNullException ("fromNoninstanciated");
                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
                }
 
@@ -337,11 +354,7 @@ namespace System
                                                     ParameterModifier[] modifiers,
                                                     CultureInfo culture, string[] namedParameters)
                {
-#if NET_2_0
                        const string bindingflags_arg = "bindingFlags";
-#else
-                       const string bindingflags_arg = "invokeAttr";
-#endif
 
 
                        if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
@@ -415,10 +428,6 @@ namespace System
                                                if (System.Reflection.Missing.Value == args [i] && (parameters [i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault)
                                                        throw new ArgumentException ("Used Missing.Value for argument without default value", "parameters");
                                        }
-                                       bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
-                                               typeof (ParamArrayAttribute)) : false;
-                                       if (hasParamArray)
-                                               ReorderParamArrayArguments (ref args, m);
                                        object result = m.Invoke (target, invokeAttr, binder, args, culture);
                                        binder.ReorderArgumentArray (ref args, state);
                                        return result;
@@ -435,10 +444,8 @@ namespace System
                        } else if ((invokeAttr & BindingFlags.SetField) != 0) {
                                FieldInfo f = GetField (name, invokeAttr);
                                if (f != null) {
-#if NET_2_0
                                        if (args == null)
                                                throw new ArgumentNullException ("providedArgs");
-#endif
                                        if ((args == null) || args.Length != 1)
                                                throw new ArgumentException ("Only the field value can be specified to set a field value.", bindingflags_arg);
                                        f.SetValue (target, args [0]);
@@ -467,11 +474,6 @@ namespace System
                                if (m == null) {
                                        throwMissingFieldException = true;
                                } else {
-                                       ParameterInfo[] parameters = m.GetParameters();
-                                       bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
-                                               typeof (ParamArrayAttribute)) : false;
-                                       if (hasParamArray)
-                                               ReorderParamArrayArguments (ref args, m);
                                        object result = m.Invoke (target, invokeAttr, binder, args, culture);
                                        binder.ReorderArgumentArray (ref args, state);
                                        return result;
@@ -495,11 +497,6 @@ namespace System
                                if (m == null) {
                                        throwMissingFieldException = true;
                                } else {
-                                       ParameterInfo[] parameters = m.GetParameters();
-                                       bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
-                                               typeof (ParamArrayAttribute)) : false;
-                                       if (hasParamArray)
-                                               ReorderParamArrayArguments (ref args, m);
                                        object result = m.Invoke (target, invokeAttr, binder, args, culture);
                                        binder.ReorderArgumentArray (ref args, state);
                                        return result;
@@ -586,7 +583,7 @@ namespace System
 
                public override MemberTypes MemberType {
                        get {
-                               if (DeclaringType != null)
+                               if (DeclaringType != null && !IsGenericParameter)
                                        return MemberTypes.NestedType;
                                else
                                        return MemberTypes.TypeInfo;
@@ -638,7 +635,6 @@ namespace System
                        return getFullName (false, false);
                }
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern override Type [] GetGenericArguments ();
 
@@ -669,9 +665,30 @@ namespace System
                        [MethodImplAttribute(MethodImplOptions.InternalCall)]
                        get;
                }
+
+               public override Type GetGenericTypeDefinition () {
+                       Type res = GetGenericTypeDefinition_impl ();
+                       if (res == null)
+                               throw new InvalidOperationException ();
+
+                       return res;
+               }
+
+#if NET_4_0
+               public override IList<CustomAttributeData> GetCustomAttributesData () {
+                       return CustomAttributeData.GetCustomAttributes (this);
+               }
+
+
+               public override Array GetEnumValues () {
+                       if (!IsEnum)
+                               throw new ArgumentException ("Type is not an enumeration", "enumType");
+
+                       return Enum.GetValues (this);
+               }
 #endif
 
-               private MethodBase CheckMethodSecurity (MethodBase mb)
+               static MethodBase CheckMethodSecurity (MethodBase mb)
                {
 #if NET_2_1
                        return mb;
@@ -689,23 +706,26 @@ namespace System
 #endif
                }
 
-               void ReorderParamArrayArguments(ref object[] args, MethodBase method)
+#if NET_4_0
+               //seclevel { transparent = 0, safe-critical = 1, critical = 2}
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               public extern int get_core_clr_security_level ();
+
+               public override bool IsSecurityTransparent
                {
-                       ParameterInfo[] parameters = method.GetParameters();
-                       object[] newArgs = new object [parameters.Length];
-                       Array paramArray = Array.CreateInstance(parameters[parameters.Length - 1].ParameterType.GetElementType(), 
-                               args.Length - (parameters.Length - 1));
-                       int paramArrayCount = 0;
-                       for (int i = 0; i < args.Length; i++) {
-                               if (i < (parameters.Length - 1))
-                                       newArgs [i] = args [i];
-                               else {
-                                       paramArray.SetValue (args [i], paramArrayCount);
-                                       paramArrayCount ++;
-                               }
-                       }
-                       newArgs [parameters.Length - 1] = paramArray;
-                       args = newArgs;
+                       get { return get_core_clr_security_level () == 0; }
                }
+
+               public override bool IsSecurityCritical
+               {
+                       get { return get_core_clr_security_level () > 0; }
+               }
+
+               public override bool IsSecuritySafeCritical
+               {
+                       get { return get_core_clr_security_level () == 1; }
+               }
+#endif
+
        }
 }