Пример #1
0
        public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
        {
            IDictionary<String, Object> dict = new Dictionary<String, Object>();
            bsonReader.ReadStartDocument();

            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var elementName = bsonReader.ReadName();

                var pi = nominalType.GetProperty(elementName);
                if (pi != null)
                {
                    dynamic data = null;
                    if (bsonReader.CurrentBsonType.Equals(BsonType.Null))
                    {
                        bsonReader.ReadNull();
                    }
                    else
                    {
                        data = BsonDefaultSerializer.Instance.GetSerializer(pi.PropertyType).Deserialize(bsonReader, pi.PropertyType, options);
                        this.GetType().GetProperty(pi.Name).SetValue(this, data, null);
                    }
                }
                else
                {
                    if (bsonReader.CurrentBsonType.Equals(BsonType.Null))
                    {
                        bsonReader.SkipValue();
                        continue;
                    }

                    if (dict.Keys.Contains("elementName"))
                    {
                        dict[elementName] = GetBsonString(ref bsonReader);
                    }
                    else
                    {
                        dict.Add(elementName, GetBsonString(ref bsonReader));
                    }
                }
            }
            bsonReader.ReadEndDocument();

            SetDynamics(dict);
            return this;
        }