Exemple #1
0
        public static void TestUniversalGenericThatDerivesFromBaseInstantiatedOverArray()
        {
            string s = null;

            // Root derived types (these types cause the virtual methods to not be in the sealed vtable)
            new OtherDerivedType <string>().BaseMethod(new string[] { "s" }, ref s);
            new OtherDerivedType <string>().MiddleMethod("s", ref s);
            new OtherDerivedTypeLeadingToNonSharedGenerics <short>().BaseMethod(123, ref s);
            new OtherDerivedTypeLeadingToNonSharedGenerics <short>().MiddleMethod(123, ref s);
            new OtherDerivedTypeWithTwoArgs <string, string>().BaseMethod(new string[] { "s" }, ref s);
            new OtherDerivedTypeWithTwoArgs <string, string>().MiddleMethod("s", ref s);
            new OtherDerivedTypeWithTwoArgsLeadingToPartialAndNonShared <string, string>().BaseMethod(11, ref s);
            new OtherDerivedTypeWithTwoArgsLeadingToPartialAndNonShared <string, string>().MiddleMethod("s", ref s);

            Assert.AreEqual(typeof(PartialUniversalGen.DerivedWithArray <>).Name, TypeOf.PUG_DerivedWithArray.Name);

            {
                var              t = TypeOf.PUG_DerivedWithArray.MakeGenericType(TypeOf.Short);
                IBase <short[]>  b = (IBase <short[]>)Activator.CreateInstance(t);
                IMiddle <short>  m = (IMiddle <short>)b;
                IDerived <short> d = (IDerived <short>)b;

                Assert.AreEqual(typeof(short[][]), b.BaseMethod(new short[0], ref s).GetType());
                Assert.AreEqual("BaseMethod", s);
                Assert.AreEqual(typeof(short[]), m.MiddleMethod(2, ref s).GetType());
                Assert.AreEqual("MiddleMethod", s);
                Assert.AreEqual(typeof(short[]), d.DerivedMethod(1, ref s).GetType());
                Assert.AreEqual("DerivedMethod", s);
            }

            {
                var             t = typeof(DerivedWithNonSharedGenerics <>).MakeGenericType(TypeOf.Long);
                IBase <int>     b = (IBase <int>)Activator.CreateInstance(t);
                IMiddle <long>  m = (IMiddle <long>)b;
                IDerived <long> d = (IDerived <long>)b;

                Assert.AreEqual(typeof(int[]), b.BaseMethod(123, ref s).GetType());
                Assert.AreEqual("BaseMethod", s);
                Assert.AreEqual(typeof(long[]), m.MiddleMethod(2, ref s).GetType());
                Assert.AreEqual("MiddleMethod", s);
                Assert.AreEqual(typeof(long[]), d.DerivedMethod(1, ref s).GetType());
                Assert.AreEqual("DerivedMethod", s);
            }

            {
                var              t = typeof(DerivedWithArrayWithTwoArgs <,>).MakeGenericType(TypeOf.Float, TypeOf.Float);
                IBase <float[]>  b = (IBase <float[]>)Activator.CreateInstance(t);
                IMiddle <float>  m = (IMiddle <float>)b;
                IDerived <float> d = (IDerived <float>)b;

                Assert.AreEqual(typeof(float[][]), b.BaseMethod(new float[0], ref s).GetType());
                Assert.AreEqual("BaseMethod", s);
                Assert.AreEqual(typeof(float[]), m.MiddleMethod(2.0f, ref s).GetType());
                Assert.AreEqual("MiddleMethod", s);
                Assert.AreEqual(typeof(float[]), d.DerivedMethod(1.0f, ref s).GetType());
                Assert.AreEqual("DerivedMethod", s);
            }

            {
                var               t = typeof(DerivedWithArrayWithTwoArgsLeadingToPartialAndNonShared <,>).MakeGenericType(TypeOf.Double, TypeOf.Double);
                IBase <int>       b = (IBase <int>)Activator.CreateInstance(t);
                IMiddle <double>  m = (IMiddle <double>)b;
                IDerived <double> d = (IDerived <double>)b;

                Assert.AreEqual(typeof(int[]), b.BaseMethod(11, ref s).GetType());
                Assert.AreEqual("BaseMethod", s);
                Assert.AreEqual(typeof(double[]), m.MiddleMethod(2.0, ref s).GetType());
                Assert.AreEqual("MiddleMethod", s);
                Assert.AreEqual(typeof(double[]), d.DerivedMethod(1.0, ref s).GetType());
                Assert.AreEqual("DerivedMethod", s);
            }
        }