Exemplo n.º 1
0
 public Choice1(VarStringArgs varArgs, params string[] parameters)
 {
     I = 6;
 }
Exemplo n.º 2
0
    public static void TestActivatorCreateInstanceBinding()
    {
        Type t;
        object[] args;
        Choice1 c1;

        // Root the constructors
        if (string.Empty.Length > 0)
        {
            new Choice1();
            new Choice1(123);
            new Choice1("Hey");
            new Choice1(5.1);
            new Choice1(new VarArgs());
            new Choice1(new VarStringArgs());
            new Choice1(new VarIntArgs());
        }

        t = null;
        args = new object[1];
        Assert.Throws<ArgumentNullException>(() => Activator.CreateInstance(t, args));

        t = typeof(Choice1);
        args = null;   // Passing a null args is equivalent to passing an empty array of args.
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 1);

        t = typeof(Choice1);
        args = new object[] { };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 1);

        t = typeof(Choice1);
        args = new object[] { 42 };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 2);

        // Primitive widening is allowed by the binder.
        // but not by Dynamic.DelegateInvoke()... 
        {
            t = typeof(Choice1);
            args = new object[] { (short)(-2) };
            c1 = (Choice1)(Activator.CreateInstance(t, args));
            Assert.Equal(c1.I, 2);
        }

        t = typeof(Choice1);
        args = new object[] { "Hello" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 3);

        t = typeof(Choice1);
        args = new object[] { null };
        Assert.Throws<AmbiguousMatchException>(() => Activator.CreateInstance(t, args));

        // "optional" parameters are not optional as far as Activator.CreateInstance() is concerned.
        t = typeof(Choice1);
        args = new object[] { 5.1 };
        Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args));

        t = typeof(Choice1);
        args = new object[] { 5.1, Type.Missing };
        Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args));

        t = typeof(Choice1);
        args = new object[] { 5.1, "Yes" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 4);

        //
        // "params" arguments are honored by Activator.CreateInstance()
        //
        VarArgs varArgs = new VarArgs();
        t = typeof(Choice1);
        args = new object[] { varArgs };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        t = typeof(Choice1);
        args = new object[] { varArgs, "P1" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        t = typeof(Choice1);
        args = new object[] { varArgs, "P1", "P2" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        VarStringArgs varStringArgs = new VarStringArgs();
        t = typeof(Choice1);
        args = new object[] { varStringArgs };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t = typeof(Choice1);
        args = new object[] { varStringArgs, "P1" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t = typeof(Choice1);
        args = new object[] { varStringArgs, "P1", "P2" };
        c1 = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t = typeof(Choice1);
        args = new object[] { varStringArgs, 5, 6 };
        Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(t, args));

        //
        // 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.)
        //
        VarIntArgs varIntArgs = new VarIntArgs();
        t = typeof(Choice1);
        args = new object[] { varIntArgs, 1, (short)2 };
        Assert.Throws<InvalidCastException>(() => Activator.CreateInstance(t, args));

        return;
    }
Exemplo n.º 3
0
 public Choice1(VarStringArgs varArgs, params string[] parameters)
 {
     I = 6;
 }
Exemplo n.º 4
0
    public static void TestActivatorCreateInstanceBinding()
    {
        Type t;

        object[] args;
        Choice1  c1;

        // Root the constructors
        if (string.Empty.Length > 0)
        {
            new Choice1();
            new Choice1(123);
            new Choice1("Hey");
            new Choice1(5.1);
            new Choice1(new VarArgs());
            new Choice1(new VarStringArgs());
            new Choice1(new VarIntArgs());
        }

        t    = null;
        args = new object[1];
        Assert.Throws <ArgumentNullException>(() => Activator.CreateInstance(t, args));

        t    = typeof(Choice1);
        args = null;   // Passing a null args is equivalent to passing an empty array of args.
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 1);

        t    = typeof(Choice1);
        args = new object[] { };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 1);

        t    = typeof(Choice1);
        args = new object[] { 42 };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 2);

        // Primitive widening is allowed by the binder.
        // but not by Dynamic.DelegateInvoke()...
        {
            t    = typeof(Choice1);
            args = new object[] { (short)(-2) };
            c1   = (Choice1)(Activator.CreateInstance(t, args));
            Assert.Equal(c1.I, 2);
        }

        t    = typeof(Choice1);
        args = new object[] { "Hello" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 3);

        t    = typeof(Choice1);
        args = new object[] { null };
        Assert.Throws <AmbiguousMatchException>(() => Activator.CreateInstance(t, args));

        // "optional" parameters are not optional as far as Activator.CreateInstance() is concerned.
        t    = typeof(Choice1);
        args = new object[] { 5.1 };
        Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args));

        t    = typeof(Choice1);
        args = new object[] { 5.1, Type.Missing };
        Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args));

        t    = typeof(Choice1);
        args = new object[] { 5.1, "Yes" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 4);

        //
        // "params" arguments are honored by Activator.CreateInstance()
        //
        VarArgs varArgs = new VarArgs();

        t    = typeof(Choice1);
        args = new object[] { varArgs };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        t    = typeof(Choice1);
        args = new object[] { varArgs, "P1" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        t    = typeof(Choice1);
        args = new object[] { varArgs, "P1", "P2" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 5);

        VarStringArgs varStringArgs = new VarStringArgs();

        t    = typeof(Choice1);
        args = new object[] { varStringArgs };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t    = typeof(Choice1);
        args = new object[] { varStringArgs, "P1" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t    = typeof(Choice1);
        args = new object[] { varStringArgs, "P1", "P2" };
        c1   = (Choice1)(Activator.CreateInstance(t, args));
        Assert.Equal(c1.I, 6);

        t    = typeof(Choice1);
        args = new object[] { varStringArgs, 5, 6 };
        Assert.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance(t, args));

        //
        // 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.)
        //
        VarIntArgs varIntArgs = new VarIntArgs();

        t    = typeof(Choice1);
        args = new object[] { varIntArgs, 1, (short)2 };
        Assert.Throws <InvalidCastException>(() => Activator.CreateInstance(t, args));

        return;
    }