Пример #1
0
        public void Multistep_Serialization_With_MemoryStream()
        {
            using (var stream = new MemoryStream())
            {
                {
                    var writer = new ObjectWriter(stream);

                    var primitiveClass1 = new PrimitiveTestClass();
                    primitiveClass1.Float   = 20.5f;
                    primitiveClass1.Integer = 77;
                    writer.Write(primitiveClass1);

                    var primitiveClass2 = new PrimitiveTestClass();
                    primitiveClass2.Float   = 86.7f;
                    primitiveClass2.Integer = 12;
                    writer.Write(primitiveClass2);
                }

                var reader = new ObjectReader(stream);
                var readPrimitiveClass1 = reader.Read() as PrimitiveTestClass;

                Assert.AreEqual(readPrimitiveClass1.Float, 20.5f);
                Assert.AreEqual(readPrimitiveClass1.Integer, 77);

                var readPrimitiveClass2 = reader.Read() as PrimitiveTestClass;

                Assert.AreEqual(readPrimitiveClass2.Float, 86.7f);
                Assert.AreEqual(readPrimitiveClass2.Integer, 12);
            }
        }
Пример #2
0
        public void Primitives_With_MemoryStream()
        {
            using (var stream = new MemoryStream())
            {
                {
                    var testObject = new PrimitiveTestClass();

                    testObject.Integer = 75;
                    testObject.Float   = 13.37f;

                    var writer = new ObjectWriter(stream);
                    writer.Write(testObject);
                }

                var reader = new ObjectReader(stream);

                var readObject = reader.Read();
                Assert.AreSame(readObject.GetType(), typeof(PrimitiveTestClass));

                var parsedObject = (PrimitiveTestClass)readObject;

                Assert.AreEqual(parsedObject.Integer, 75);
                Assert.AreEqual(parsedObject.Float, 13.37f);
            }
        }
        public void Multistep_Serialization_References_Static_NonStatic_With_MemoryStream()
        {
            using (var stream = new MemoryStream())
            {
                {
                    var myClass = new PrimitiveTestClass();

                    StaticPrimitiveContainer.TestClass         = myClass;
                    StaticPrimitiveContainer.TestClass.Float   = 5.7f;
                    StaticPrimitiveContainer.TestClass.Integer = 32;

                    var assembly = Assembly.GetExecutingAssembly();

                    var writer = new ObjectWriter(stream);
                    writer.WriteStatics(assembly);

                    writer.Write(myClass);
                }

                var reader = new ObjectReader(stream);
                reader.ReadStatics();

                Assert.AreEqual(StaticPrimitiveContainer.TestClass.Float, 5.7f);

                var readClass = reader.Read() as PrimitiveTestClass;
                Assert.AreSame(readClass, StaticPrimitiveContainer.TestClass);
            }
        }
Пример #4
0
        public void Generic_Dictionary_With_Key_References_MemoryStream()
        {
            using (var stream = new MemoryStream())
            {
                {
                    var dictionary     = new Dictionary <PrimitiveTestClass, int>();
                    var firstPrimitive = new PrimitiveTestClass();
                    firstPrimitive.Float = 70.5f;

                    dictionary[firstPrimitive] = 1;

                    var secondPrimitive = new PrimitiveTestClass();
                    secondPrimitive.Float = 85.7f;

                    dictionary[secondPrimitive] = 0;

                    var writer = new ObjectWriter(stream);
                    writer.Write(dictionary);

                    writer.Write(firstPrimitive);
                    writer.Write(secondPrimitive);
                }

                var reader = new ObjectReader(stream);

                var readDictionary = reader.Read() as Dictionary <PrimitiveTestClass, int>;

                var firstReadPrimitive  = reader.Read() as PrimitiveTestClass;
                var secondReadPrimitive = reader.Read() as PrimitiveTestClass;

                var keyEnumerator = readDictionary.Keys.GetEnumerator();
                keyEnumerator.MoveNext();

                Assert.AreSame(keyEnumerator.Current, firstReadPrimitive);

                keyEnumerator.MoveNext();
                Assert.AreSame(keyEnumerator.Current, secondReadPrimitive);

                Assert.True(readDictionary.ContainsKey(firstReadPrimitive));
                Assert.True(readDictionary.ContainsKey(secondReadPrimitive));

                Assert.AreEqual(readDictionary[firstReadPrimitive], 1);
                Assert.AreEqual(readDictionary[secondReadPrimitive], 0);
            }
        }
Пример #5
0
 public ReadOnlyOwnerClass()
 {
     test       = new PrimitiveTestClass();
     test.Float = 5;
 }
Пример #6
0
    static int Main()
    {
        BindingFlags      flags    = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
        MethodInfo        mi       = typeof(NullableTestClass).GetMethod("F");
        NullableTestClass nullable = new NullableTestClass();
        SimpleStruct?     test     = new SimpleStruct(90, 90);


        mi.Invoke(nullable, flags, new PassesStuffBinder(null), new object [] { null }, null);
        if (nullable.hasValue)
        {
            Console.WriteLine("invoked nullabled with null arg but did not get a null in the method");
            return(1);
        }


        nullable = new NullableTestClass();
        mi.Invoke(nullable, flags, new PassesStuffBinder(new SimpleStruct(10, 20)), new object [] { 200 }, null);
        if (!nullable.hasValue || nullable.bVal != 20)
        {
            Console.WriteLine("invoked nullabled with boxed struct, but did not get it");
            return(2);
        }


        nullable = new NullableTestClass();
        mi.Invoke(nullable, flags, new PassesStuffBinder(test), new object [] { 200 }, null);
        if (!nullable.hasValue || nullable.bVal != 90)
        {
            Console.WriteLine("invoked nullabled with nullable literal, but did not get it");
            return(3);
        }

        mi = typeof(PrimitiveTestClass).GetMethod("i4");
        PrimitiveTestClass prim = new PrimitiveTestClass();

        mi.Invoke(prim, flags, new PassesStuffBinder((byte)10), new object [] { 88 }, null);
        if (prim.val != 10)
        {
            Console.WriteLine("invoked primitive with byte, it should be widened to int " + prim.val);
            return(4);
        }

        try {
            mi.Invoke(prim, flags, new PassesStuffBinder(Missing.Value), new object [] { null }, null);
            Console.WriteLine("invoked literal with reference value");
            return(5);
        } catch (Exception) {
        }

        try {
            MethodInfo method         = typeof(Driver).GetMethod("StructTest");
            MethodInfo generic_method = method.MakeGenericMethod(typeof(int));
            generic_method.Invoke(null, new object [] { new GenericStruct <int>() });

            method         = typeof(Driver).GetMethod("ReferenceTest");
            generic_method = method.MakeGenericMethod(typeof(int));
            generic_method.Invoke(null, new object [] { new GenericClass <int>() });
        } catch (Exception e) {
            Console.WriteLine("calling with generic arg failed " + e);
            return(6);
        }

        return(0);
    }
        public void GetPrimitive()
        {
            PrimitiveTestClass c = new PrimitiveTestClass();

            Assert.Equal<ulong>(0, c.unsignedLong);
            Assert.Equal(-1, c.integer);
            Assert.Equal(0, c.character);
            Assert.Equal(false, c.boolean);
        }