Exemple #1
0
        public void ReadInt32_BlittableObject_ReturnsExpected()
        {
            int offset1 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value1)).ToInt32();
            int offset2 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value2)).ToInt32();

            object structure = new BlittableStruct
            {
                value1 = 10,
                value2 = 20
            };

            Assert.Equal(10, Marshal.ReadInt32(structure, offset1));
            Assert.Equal(20, Marshal.ReadInt32(structure, offset2));
        }
Exemple #2
0
        public void WriteIntPtr_BlittableObject_Roundtrips()
        {
            int offset1 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value1)).ToInt32();
            int offset2 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value2)).ToInt32();

            object structure = new BlittableStruct
            {
                value1 = (IntPtr)10,
                value2 = (IntPtr)20
            };

            Marshal.WriteIntPtr(structure, offset1, (IntPtr)11);
            Marshal.WriteIntPtr(structure, offset2, (IntPtr)21);

            Assert.Equal((IntPtr)11, ((BlittableStruct)structure).value1);
            Assert.Equal((IntPtr)21, ((BlittableStruct)structure).value2);
            Assert.Equal((IntPtr)11, Marshal.ReadIntPtr(structure, offset1));
            Assert.Equal((IntPtr)21, Marshal.ReadIntPtr(structure, offset2));
        }
Exemple #3
0
        public void WriteByte_BlittableObject_Roundtrips()
        {
            int offset1 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value1)).ToInt32();
            int offset2 = Marshal.OffsetOf <BlittableStruct>(nameof(BlittableStruct.value2)).ToInt32();

            object structure = new BlittableStruct
            {
                value1 = 10,
                value2 = 20
            };

            Marshal.WriteByte(structure, offset1, 11);
            Marshal.WriteByte(structure, offset2, 21);

            Assert.Equal(11, ((BlittableStruct)structure).value1);
            Assert.Equal(21, ((BlittableStruct)structure).value2);
            Assert.Equal(11, Marshal.ReadByte(structure, offset1));
            Assert.Equal(21, Marshal.ReadByte(structure, offset2));
        }
Exemple #4
0
        public void TestBlittableStruct()
        {
            BlittableStruct blittableStruct = new BlittableStruct();

            blittableStruct._a = 200;
            blittableStruct._b = 300;
            blittableStruct._c = 10;
            blittableStruct._d = 123;
            blittableStruct._p = new IntPtr(100);

            object boxedBlittableStruct = (object)blittableStruct;

            int offsetOfB = Marshal.OffsetOf <BlittableStruct>("_b").ToInt32();
            int offsetOfC = Marshal.OffsetOf <BlittableStruct>("_c").ToInt32();
            int offsetOfD = Marshal.OffsetOf <BlittableStruct>("_d").ToInt32();
            int offsetOfP = Marshal.OffsetOf <BlittableStruct>("_p").ToInt32();

            Assert.Equal(200, Marshal.ReadInt32(boxedBlittableStruct, 0));
            Assert.Equal(300, Marshal.ReadInt32(boxedBlittableStruct, offsetOfB));
            Assert.Equal(10, Marshal.ReadByte(boxedBlittableStruct, offsetOfC));
            Assert.Equal(123, Marshal.ReadInt16(boxedBlittableStruct, offsetOfD));
            Assert.Equal(new IntPtr(100), Marshal.ReadIntPtr(boxedBlittableStruct, offsetOfP));

            Marshal.WriteInt32(boxedBlittableStruct, 0, 300);
            Marshal.WriteInt32(boxedBlittableStruct, offsetOfB, 400);
            Marshal.WriteByte(boxedBlittableStruct, offsetOfC, 20);
            Marshal.WriteInt16(boxedBlittableStruct, offsetOfD, 144);

            Marshal.WriteIntPtr(boxedBlittableStruct, offsetOfP, new IntPtr(500));

            Assert.Equal(300, ((BlittableStruct)boxedBlittableStruct)._a);
            Assert.Equal(400, ((BlittableStruct)boxedBlittableStruct)._b);
            Assert.Equal(20, ((BlittableStruct)boxedBlittableStruct)._c);
            Assert.Equal(144, ((BlittableStruct)boxedBlittableStruct)._d);
            Assert.Equal(new IntPtr(500), ((BlittableStruct)boxedBlittableStruct)._p);
        }