public void Activator_CreateInstance_ConstructorWithPrimitive_PerformsPrimitiveWidening() { // Primitive widening is allowed by the binder, but not by Dynamic.DelegateInvoke(). Choice1 c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { (short)-2 })); Assert.AreEqual(2, c.I); }
//[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsInvokingStaticConstructorsSupported))] static void TestingBindingFlags() { AssertExtensions.Throws <MissingMethodException>(() => Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.CurrentCulture)); AssertExtensions.Throws <MissingMethodException>(() => Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Public | BindingFlags.Instance, null, new object[] { 1, 2, 3 }, CultureInfo.CurrentCulture)); Flag.Reset(0); Assert.AreEqual(0, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Static | BindingFlags.NonPublic, null, null, CultureInfo.CurrentCulture); Assert.AreEqual(300, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Instance | BindingFlags.NonPublic, null, null, CultureInfo.CurrentCulture); Assert.AreEqual(500, Flag.cnt); Flag.Reset(0); Assert.AreEqual(0, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor2), BindingFlags.Instance | BindingFlags.NonPublic, null, null, CultureInfo.CurrentCulture); Assert.AreEqual(300, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor2), BindingFlags.Static | BindingFlags.NonPublic, null, null, CultureInfo.CurrentCulture); Assert.AreEqual(500, Flag.cnt); Flag.Reset(0); Assert.AreEqual(0, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor3), BindingFlags.Instance | BindingFlags.Public, null, new object[] { 122 }, CultureInfo.CurrentCulture); Assert.AreEqual(400, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor3), BindingFlags.Static | BindingFlags.NonPublic, null, null, CultureInfo.CurrentCulture); Assert.AreEqual(600, Flag.cnt); Activator.CreateInstance(typeof(ClassWithPrivateCtor3), BindingFlags.Instance | BindingFlags.Public, null, new object[] { 122 }, CultureInfo.CurrentCulture); Assert.AreEqual(900, Flag.cnt); AssertExtensions.Throws <MissingMethodException>(() => Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Public | BindingFlags.Static, null, null, CultureInfo.CurrentCulture)); AssertExtensions.Throws <MissingMethodException>(() => Activator.CreateInstance(typeof(ClassWithPrivateCtor), BindingFlags.Public | BindingFlags.Static, null, new object[] { 122 }, CultureInfo.CurrentCulture)); }
public void Activator_CreateInstance_Generic() { Choice1 c = Activator.CreateInstance <Choice1>(); Assert.AreEqual(1, c.I); Activator.CreateInstance <DateTime>(); Activator.CreateInstance <StructTypeWithoutReflectionMetadata>(); }
public void Activator_TestActivatorOnNonActivatableFinalizableTypes() { // On runtimes where the generic Activator is implemented with special codegen intrinsics, we might allocate // an uninitialized instance of the object before we realize there's no default constructor to run. // Make sure this has no observable side effects. AssertExtensions.ThrowsAny <MissingMemberException>(() => { Activator.CreateInstance <TypeWithPrivateDefaultCtorAndFinalizer>(); }); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Assert.IsFalse(TypeWithPrivateDefaultCtorAndFinalizer.WasCreated); }
public void Activator_CreateInstance_Invalid() { Type nullType = null; AssertExtensions.Throws <ArgumentNullException>("type", () => Activator.CreateInstance(nullType)); // Type is null AssertExtensions.Throws <ArgumentNullException>("type", () => Activator.CreateInstance(null, new object[0])); // Type is null AssertExtensions.Throws <AmbiguousMatchException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { null })); // C# designated optional parameters are not optional as far as Activator.CreateInstance() is concerned. AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { 5.1 })); AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { 5.1, Type.Missing })); // Invalid params args AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { new VarStringArgs(), 5, 6 })); // Primitive widening not supported for "params" arguments. // // (This is probably an accidental behavior on the desktop as the default binder specifically checks to see if the params arguments are widenable to the // params array element type and gives it the go-ahead if it is. Unfortunately, the binder then bollixes itself by using Array.Copy() to copy // the params arguments. Since Array.Copy() doesn't tolerate this sort of type mismatch, it throws an InvalidCastException which bubbles out // out of Activator.CreateInstance. Accidental or not, we'll inherit that behavior on .NET Native.) #if !WindowsCE AssertExtensions.Throws <InvalidCastException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { new VarIntArgs(), 1, (short)2 })); #else Assert.IsNotNull(Activator.CreateInstance(typeof(Choice1), new object[] { new VarIntArgs(), 1, (short)2 })); #endif AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(TypeWithoutDefaultCtor))); // Type has no default constructor AssertExtensions.Throws <TargetInvocationException>(() => Activator.CreateInstance(typeof(TypeWithDefaultCtorThatThrows))); // Type has a default constructor throws an exception #if !WindowsCE AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(AbstractTypeWithDefaultCtor))); // Type is abstract AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(typeof(IInterfaceType))); // Type is an interface #else AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance(typeof(AbstractTypeWithDefaultCtor))); // Type is abstract AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance(typeof(IInterfaceType))); // Type is an interface #endif #if netcoreapp || uapaot foreach (Type nonRuntimeType in Helpers.NonRuntimeTypes) { // Type is not a valid RuntimeType AssertExtensions.Throws <ArgumentException>("type", () => Activator.CreateInstance(nonRuntimeType)); } #endif // netcoreapp || uapaot }
public void Activator_CreateInstance_Generic_Invalid() { #if !WindowsCE AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <int[]>()); // Cannot create array type #else Activator.CreateInstance <int[]>(); // Can create array type #endif AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <TypeWithoutDefaultCtor>()); // Type has no default constructor AssertExtensions.Throws <TargetInvocationException>(() => Activator.CreateInstance <TypeWithDefaultCtorThatThrows>()); // Type has a default constructor that throws #if !WindowsCE AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <AbstractTypeWithDefaultCtor>()); // Type is abstract AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <IInterfaceType>()); // Type is an interface #else AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance <AbstractTypeWithDefaultCtor>()); // Type is abstract AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance <IInterfaceType>()); // Type is an interface #endif }
public void Activator_CreateInstance() { // Passing null args is equivalent to an empty array of args. Choice1 c = (Choice1)(Activator.CreateInstance(typeof(Choice1), null)); Assert.AreEqual(1, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { })); Assert.AreEqual(1, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { 42 })); Assert.AreEqual(2, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { "Hello" })); Assert.AreEqual(3, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { 5.1, "Hello" })); Assert.AreEqual(4, c.I); Activator.CreateInstance(typeof(StructTypeWithoutReflectionMetadata)); }
public void Activator_CreateInstance_ConstructorWithParamsParameter() { // C# params arguments are honored by Activator.CreateInstance() Choice1 c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarArgs() })); Assert.AreEqual(5, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarArgs(), "P1" })); Assert.AreEqual(5, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarArgs(), "P1", "P2" })); Assert.AreEqual(5, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarStringArgs() })); Assert.AreEqual(6, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarStringArgs(), "P1" })); Assert.AreEqual(6, c.I); c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { new VarStringArgs(), "P1", "P2" })); Assert.AreEqual(6, c.I); }