Activator cannot be used to create default instances of user types
authorMarek Safar <marek.safar@gmail.com>
Tue, 5 Mar 2013 18:16:07 +0000 (19:16 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 6 Mar 2013 10:18:03 +0000 (11:18 +0100)
mcs/class/corlib/System/Activator.cs
mcs/class/corlib/Test/System/ActivatorTest.cs

index abd9ef92f207b5d089d6226b2c5258399e054582..9e999435e17c11c64b5b4fc759b66bf946d0e922 100644 (file)
@@ -306,20 +306,15 @@ namespace System
                        if (type.ContainsGenericParameters)
                                throw new ArgumentException (type + " is an open generic type", "type");
 
-                       CheckAbstractType (type);
+                       MonoType monoType = type.UnderlyingSystemType as MonoType;
+                       if (monoType == null)
+                               throw new ArgumentException ("Type must be a type provided by the runtime");
+
+                       CheckAbstractType (monoType);
 
-                       ConstructorInfo ctor;
-                       MonoType monoType = type as MonoType;
-
-                       if (monoType != null) {
-                               ctor = monoType.GetDefaultConstructor ();
-                               if (!nonPublic && ctor != null && !ctor.IsPublic)
-                                       ctor = null;
-                       } else {
-                               BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
-                               if (nonPublic)
-                                       flags |= BindingFlags.NonPublic;
-                               ctor = type.GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null);
+                       var ctor = monoType.GetDefaultConstructor ();
+                       if (!nonPublic && ctor != null && !ctor.IsPublic) {
+                               ctor = null;
                        }
 
                        if (ctor == null) {
index 130d8d2518d6daced90a5396254a12a265d52def..1b8108743221be2673c301b7e778b4c3a1a07f9c 100644 (file)
@@ -61,6 +61,192 @@ namespace MonoTests.System {
 
        using MonoTests.System.ActivatorTestInternal;
 
+       class CustomUserType : Type
+       {
+               public override Assembly Assembly
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override string AssemblyQualifiedName
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override Type BaseType
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override string FullName
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override Guid GUID
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               protected override TypeAttributes GetAttributeFlagsImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetElementType ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override EventInfo[] GetEvents (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override FieldInfo GetField (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override FieldInfo[] GetFields (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetInterface (string name, bool ignoreCase)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type[] GetInterfaces ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetNestedType (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type[] GetNestedTypes (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool HasElementTypeImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsArrayImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsByRefImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsCOMObjectImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsPointerImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsPrimitiveImpl ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Module Module
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override string Namespace
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override Type UnderlyingSystemType
+               {
+                       get {
+                               return this;
+                       }
+               }
+
+               public override object[] GetCustomAttributes (Type attributeType, bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override object[] GetCustomAttributes (bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override bool IsDefined (Type attributeType, bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override string Name
+               {
+                       get { throw new NotImplementedException (); }
+               }
+       }
+
+
        [TestFixture]
        public class ActivatorTest {
 
@@ -80,6 +266,13 @@ namespace MonoTests.System {
                        Activator.CreateInstance ((Type)null);
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void CreateInstance_CustomType ()
+               {
+                       Activator.CreateInstance (new CustomUserType ());
+               }
+
                [Test]
                public void CreateInstance_StringString ()
                {