/// <summary>
        /// Reads one object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="index"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected NdfObject ReadObject(Stream ms, uint index, NdfBinary owner)
        {
            var instance = new NdfObject {
                Id = index
            };

            if (owner.TopObjects.Contains(index))
            {
                instance.IsTopObject = true;
            }

            var buffer = new byte[4];

            ms.Read(buffer, 0, buffer.Length);
            int classId = BitConverter.ToInt32(buffer, 0);

            if (owner.Classes.Count < classId)
            {
                throw new InvalidDataException("Object without class found.");
            }

            NdfClass cls = instance.Class = owner.Classes[classId];

            cls.Instances.Add(instance);

            // Read properties
            for (; ;)
            {
                ms.Read(buffer, 0, buffer.Length);
                uint propertyId = BitConverter.ToUInt32(buffer, 0);

                if (propertyId == 0xABABABAB)
                {
                    break;
                }

                var propVal = new NdfPropertyValue(instance)
                {
                    Property = cls.Properties.SingleOrDefault(x => x.Id == propertyId)
                };

                if (propVal.Property == null)
                {
                    // throw new InvalidDataException("Found a value for a property which doens't exist in this class.");
                    foreach (var c in owner.Classes)
                    {
                        foreach (var p in c.Properties)
                        {
                            if (p.Id == propertyId)
                            {
                                propVal.Property = p;
                                break;
                            }
                        }
                    }
                }

                instance.PropertyValues.Add(propVal);

                NdfValueWrapper res = ReadValue(ms, owner);
                propVal.Value = res;
            }

            owner.AddEmptyProperties(instance);

            return(instance);
        }