public void TestCompositeArray()
        {
            // 1. Test simple array.
            object[] inArr = { new CompositeInner(1) };

            IBinaryObject binObj = _grid.GetBinary().GetBuilder(typeof(CompositeArray)).SetHashCode(100)
                .SetField("inArr", inArr).Build();

            IBinaryType meta = binObj.GetBinaryType();

            Assert.AreEqual(typeof(CompositeArray).Name, meta.TypeName);
            Assert.AreEqual(1, meta.Fields.Count);
            Assert.AreEqual(BinaryTypeNames.TypeNameArrayObject, meta.GetFieldTypeName("inArr"));

            Assert.AreEqual(100, binObj.GetHashCode());

            var binInArr = binObj.GetField<IBinaryObject[]>("inArr").ToArray();

            Assert.AreEqual(1, binInArr.Length);
            Assert.AreEqual(1, binInArr[0].GetField<int>("val"));

            CompositeArray arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(1, arr.InArr.Length);
            Assert.AreEqual(1, ((CompositeInner) arr.InArr[0]).Val);

            // 2. Test addition to array.
            binObj = _grid.GetBinary().GetBuilder(binObj).SetHashCode(200)
                .SetField("inArr", new[] { binInArr[0], null }).Build();

            Assert.AreEqual(200, binObj.GetHashCode());

            binInArr = binObj.GetField<IBinaryObject[]>("inArr").ToArray();

            Assert.AreEqual(2, binInArr.Length);
            Assert.AreEqual(1, binInArr[0].GetField<int>("val"));
            Assert.IsNull(binInArr[1]);

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, ((CompositeInner) arr.InArr[0]).Val);
            Assert.IsNull(arr.InArr[1]);

            binInArr[1] = _grid.GetBinary().GetBuilder(typeof(CompositeInner)).SetField("val", 2).Build();

            binObj = _grid.GetBinary().GetBuilder(binObj).SetHashCode(300)
                .SetField("inArr", binInArr.OfType<object>().ToArray()).Build();

            Assert.AreEqual(300, binObj.GetHashCode());

            binInArr = binObj.GetField<IBinaryObject[]>("inArr").ToArray();

            Assert.AreEqual(2, binInArr.Length);
            Assert.AreEqual(1, binInArr[0].GetField<int>("val"));
            Assert.AreEqual(2, binInArr[1].GetField<int>("val"));

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, ((CompositeInner)arr.InArr[0]).Val);
            Assert.AreEqual(2, ((CompositeInner)arr.InArr[1]).Val);

            // 3. Test top-level handle inversion.
            CompositeInner inner = new CompositeInner(1);

            inArr = new object[] { inner, inner };

            binObj = _grid.GetBinary().GetBuilder(typeof(CompositeArray)).SetHashCode(100)
                .SetField("inArr", inArr).Build();

            Assert.AreEqual(100, binObj.GetHashCode());

            binInArr = binObj.GetField<IBinaryObject[]>("inArr").ToArray();

            Assert.AreEqual(2, binInArr.Length);
            Assert.AreEqual(1, binInArr[0].GetField<int>("val"));
            Assert.AreEqual(1, binInArr[1].GetField<int>("val"));

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(1, ((CompositeInner)arr.InArr[0]).Val);
            Assert.AreEqual(1, ((CompositeInner)arr.InArr[1]).Val);

            binInArr[0] = _grid.GetBinary().GetBuilder(typeof(CompositeInner)).SetField("val", 2).Build();

            binObj = _grid.GetBinary().GetBuilder(binObj).SetHashCode(200)
                .SetField("inArr", binInArr.ToArray<object>()).Build();

            Assert.AreEqual(200, binObj.GetHashCode());

            binInArr = binObj.GetField<IBinaryObject[]>("inArr").ToArray();

            Assert.AreEqual(2, binInArr.Length);
            Assert.AreEqual(2, binInArr[0].GetField<int>("val"));
            Assert.AreEqual(1, binInArr[1].GetField<int>("val"));

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.OutArr);
            Assert.AreEqual(2, arr.InArr.Length);
            Assert.AreEqual(2, ((CompositeInner)arr.InArr[0]).Val);
            Assert.AreEqual(1, ((CompositeInner)arr.InArr[1]).Val);

            // 4. Test nested object handle inversion.
            CompositeOuter[] outArr = { new CompositeOuter(inner), new CompositeOuter(inner) };

            binObj = _grid.GetBinary().GetBuilder(typeof(CompositeArray)).SetHashCode(100)
                .SetField("outArr", outArr.ToArray<object>()).Build();

            meta = binObj.GetBinaryType();

            Assert.AreEqual(typeof(CompositeArray).Name, meta.TypeName);
            Assert.AreEqual(2, meta.Fields.Count);
            Assert.AreEqual(BinaryTypeNames.TypeNameArrayObject, meta.GetFieldTypeName("inArr"));
            Assert.AreEqual(BinaryTypeNames.TypeNameArrayObject, meta.GetFieldTypeName("outArr"));

            Assert.AreEqual(100, binObj.GetHashCode());

            var binOutArr = binObj.GetField<IBinaryObject[]>("outArr").ToArray();

            Assert.AreEqual(2, binOutArr.Length);
            Assert.AreEqual(1, binOutArr[0].GetField<IBinaryObject>("inner").GetField<int>("val"));
            Assert.AreEqual(1, binOutArr[1].GetField<IBinaryObject>("inner").GetField<int>("val"));

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.InArr);
            Assert.AreEqual(2, arr.OutArr.Length);
            Assert.AreEqual(1, ((CompositeOuter) arr.OutArr[0]).Inner.Val);
            Assert.AreEqual(1, ((CompositeOuter) arr.OutArr[0]).Inner.Val);

            binOutArr[0] = _grid.GetBinary().GetBuilder(typeof(CompositeOuter))
                .SetField("inner", new CompositeInner(2)).Build();

            binObj = _grid.GetBinary().GetBuilder(binObj).SetHashCode(200)
                .SetField("outArr", binOutArr.ToArray<object>()).Build();

            Assert.AreEqual(200, binObj.GetHashCode());

            binInArr = binObj.GetField<IBinaryObject[]>("outArr").ToArray();

            Assert.AreEqual(2, binInArr.Length);
            Assert.AreEqual(2, binOutArr[0].GetField<IBinaryObject>("inner").GetField<int>("val"));
            Assert.AreEqual(1, binOutArr[1].GetField<IBinaryObject>("inner").GetField<int>("val"));

            arr = binObj.Deserialize<CompositeArray>();

            Assert.IsNull(arr.InArr);
            Assert.AreEqual(2, arr.OutArr.Length);
            Assert.AreEqual(2, ((CompositeOuter)arr.OutArr[0]).Inner.Val);
            Assert.AreEqual(1, ((CompositeOuter)arr.OutArr[1]).Inner.Val);
        }
        public void TestCompositeContainer()
        {
            ArrayList col = new ArrayList();
            IDictionary dict = new Hashtable();

            col.Add(new CompositeInner(1));
            dict[3] = new CompositeInner(3);

            IBinaryObject binObj = _grid.GetBinary().GetBuilder(typeof(CompositeContainer)).SetHashCode(100)
                .SetCollectionField("col", col)
                .SetDictionaryField("dict", dict).Build();

            // 1. Check meta.
            IBinaryType meta = binObj.GetBinaryType();

            Assert.AreEqual(typeof(CompositeContainer).Name, meta.TypeName);

            Assert.AreEqual(2, meta.Fields.Count);
            Assert.AreEqual(BinaryTypeNames.TypeNameCollection, meta.GetFieldTypeName("col"));
            Assert.AreEqual(BinaryTypeNames.TypeNameMap, meta.GetFieldTypeName("dict"));

            // 2. Check in binary form.
            Assert.AreEqual(1, binObj.GetField<ICollection>("col").Count);
            Assert.AreEqual(1, binObj.GetField<ICollection>("col").OfType<IBinaryObject>().First()
                .GetField<int>("val"));

            Assert.AreEqual(1, binObj.GetField<IDictionary>("dict").Count);
            Assert.AreEqual(3, ((IBinaryObject) binObj.GetField<IDictionary>("dict")[3]).GetField<int>("val"));

            // 3. Check in deserialized form.
            CompositeContainer obj = binObj.Deserialize<CompositeContainer>();

            Assert.AreEqual(1, obj.Col.Count);
            Assert.AreEqual(1, obj.Col.OfType<CompositeInner>().First().Val);

            Assert.AreEqual(1, obj.Dict.Count);
            Assert.AreEqual(3, ((CompositeInner) obj.Dict[3]).Val);
        }
 public CompositeOuter(CompositeInner inner)
 {
     Inner = inner;
 }