示例#1
0
        /// <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;
        }
 private void GetValue(IValueHolder valueHolder, NdfObjectViewModel inst, List<NdfPropertyValue> result, NdfPropertyValue propertyValue)
 {
     switch (valueHolder.Value.Type)
     {
         case NdfType.ObjectReference:
             var ndfObjectReference = valueHolder.Value as NdfObjectReference;
             if (ndfObjectReference != null && ndfObjectReference.InstanceId == inst.Object.Id)
                 result.Add(propertyValue);
             break;
         case NdfType.List:
         case NdfType.MapList:
             var ndfCollection = valueHolder.Value as NdfCollection;
             if (ndfCollection != null)
                 foreach (var col in ndfCollection)
                     GetValue(col, inst, result, propertyValue);
             break;
         case NdfType.Map:
             var map = valueHolder.Value as NdfMap;
             if (map != null)
             {
                 GetValue(map.Key, inst, result, propertyValue);
                 GetValue(map.Value as IValueHolder, inst, result, propertyValue);
             }
             break;
     }
 }