Beispiel #1
0
        void TestValue(bool selector)
        {
            ValX v1 = new ValX();
            RefX v2 = new RefX();

            Change(selector ? (IfcX)v1 : (IfcX)v2);

            void Change(IfcX x) => x.Change();
        }
Beispiel #2
0
        void TestArrayCast()
        {
            var intArr = new int[3];
            var refArr = new RefX[3];
            var valArr = new ValX[3];

            Console.WriteLine("Array casts: "
                              + (((object)intArr) is object[])
                              + "," + (((object)intArr) is ValueType[])
                              + "," + (((object)refArr) is object[])
                              + "," + (((object)refArr) is ValueType[])
                              + "," + (((object)valArr) is object[])
                              + "," + (((object)valArr) is ValueType[]));
            try
            {
                var objArr = (IfcX[])((object)valArr);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception " + e.GetType());
            }
        }
Beispiel #3
0
        void TestArrayCast()
        {
            var intArr = new int[3];
            var refArr = new RefX[3];
            var valArr = new ValX[3];

            Console.WriteLine("Array casts: "
                              + (((object)intArr) is object[])
                              + "," + (((object)intArr) is ValueType[])
                              + "," + (((object)refArr) is object[])
                              + "," + (((object)refArr) is ValueType[])
                              + "," + (((object)valArr) is object[])
                              + "," + (((object)valArr) is ValueType[]));
            try
            {
                var objArr = (IfcX[])((object)valArr);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception " + e.GetType());
            }

            // test generic cast

            var tupleArray = new Tuple <int, string>[3] {
                new Tuple <int, string>(1, "one"),
                new Tuple <int, string>(2, "two"),
                new Tuple <int, string>(3, "three"),
            };
            var tupleEnum =
                ((System.Collections.Generic.IEnumerable <Tuple <int, string> >)tupleArray)
                .GetEnumerator();

            while (tupleEnum.MoveNext())
            {
                System.Console.Write(tupleEnum.Current);
            }
            System.Console.WriteLine();
        }
Beispiel #4
0
        void TestLoadStore()
        {
            var intArr = new int[5];
            var refArr = new RefX[5];
            var valArr = new ValX[5];

            /*
             * Console.WriteLine("RefX castable to IfcArray? " + (refArr is IfcX[])
             + "," + (refArr is System.Collections.Generic.IList<IfcX>));
             + Console.WriteLine("ValX castable to IfcArray? " + (valArr is IfcX[])
             + "," + (valArr is System.Collections.Generic.IList<IfcX>));
             */

            intArr[3] = 10;
            refArr[3] = new RefX {
                x = 20
            };
            valArr[3] = new ValX {
                x = 30
            };

            var ref3 = refArr[3];
            var val3 = valArr[3];

            Change(refArr[3]);
            Change(valArr[3]);
            ref3.x *= 2;
            val3.x *= 2;

            Console.Write(ref3.x + "," + val3.x + "," + intArr[3] + "," + refArr[3].x + "," + valArr[3].x);
            valArr[4] = val3;
            val3      = default(ValX);
            Console.Write(" AND " + valArr[4].Print());

            valArr[3].Change();
            Console.Write(" AND " + valArr[3].Print());

            IfcX ifc3 = valArr[3];

            ifc3.Change();
            Console.Write(" AND " + valArr[3].Print());

            var ifcArr = new IfcX[3];

            ifcArr[1] = valArr[3];
            ifcArr[1].Change();
            Console.Write(" AND " + valArr[3].Print());

            void Change(IfcX x) => x.Change();

            TestGeneric <RefX>();
            TestGeneric <ValX>();
            Console.WriteLine();

            TestGeneric2 <int>();
            TestGeneric2 <bool>();
            TestGeneric2 <Exception>();
            Console.WriteLine();

            //

            object valBox  = valArr[1];
            IfcX   valBox2 = (IfcX)valBox;
            IfcX   valBox3 = valArr[1];
            ValX   valBox4 = (ValX)valBox;

            ((ValX)valBox).Change();
            valBox2.Change();
            Console.WriteLine("BOX " + valArr[1].Print() + " , " + ((IfcX)valBox).Print() + " , " + valBox2.Print() + " , " + valBox3.Print() + " , " + valBox4.Print());

            // test invalid store

            try { ((object[])(new Version[4]))[0] = new ValX(); }
            catch (Exception e) { Console.WriteLine(e.GetType()); }

            void TestGeneric <T>() where T : IfcX, new()
            {
                T[] arr = new T[5];
                arr[3] = new T();
                var x = arr[3];

                x.Change();
                arr[3].Change();
                Console.Write(" ; " + x.Print() + " vs " + arr[3].Print());
            }

            void TestGeneric2 <T>()
            {
                T[] arr = new T[5];
                arr[3] = default(T);
                foreach (var e in arr)
                {
                    Console.Write("[" + (e == null ? "null" : e.ToString()) + "]");
                }
            }
        }