MongoDB.Bson.Serialization.Serializers.Int32Serializer.Deserialize C# (CSharp) Метод

Deserialize() публичный Метод

Deserializes an object from a BsonReader.
public Deserialize ( MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options ) : object
bsonReader MongoDB.Bson.IO.BsonReader The BsonReader.
nominalType System.Type The nominal type of the object.
actualType System.Type The actual type of the object.
options IBsonSerializationOptions The serialization options.
Результат object
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(int));
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Double:
                    return representationSerializationOptions.ToInt32(bsonReader.ReadDouble());
                case BsonType.Int32:
                    return bsonReader.ReadInt32();
                case BsonType.Int64:
                    return representationSerializationOptions.ToInt32(bsonReader.ReadInt64());
                case BsonType.String:
                    return XmlConvert.ToInt32(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize Int32 from BsonType {0}.", bsonType);
                    throw new Exception(message);
            }
        }

Usage Example

        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>An object.</returns>
        protected override Version DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;

            BsonType bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Document:
                int major = 0, minor = 0, build = 0, revision = 0;
                var foundMemberFlags = _helper.DeserializeMembers(context, (elementName, flag) =>
                {
                    switch (flag)
                    {
                    case Flags.Major: major = _int32Serializer.Deserialize(context); break;

                    case Flags.Minor: minor = _int32Serializer.Deserialize(context); break;

                    case Flags.Build: build = _int32Serializer.Deserialize(context); break;

                    case Flags.Revision: revision = _int32Serializer.Deserialize(context); break;
                    }
                });
                switch (foundMemberFlags)
                {
                case Flags.MajorMinor: return(new Version(major, minor));

                case Flags.MajorMinorBuild: return(new Version(major, minor, build));

                case Flags.All: return(new Version(major, minor, build, revision));

                default: throw new BsonInternalException();
                }

            case BsonType.String:
                return(new Version(bsonReader.ReadString()));

            default:
                throw CreateCannotDeserializeFromBsonTypeException(bsonType);
            }
        }
All Usage Examples Of MongoDB.Bson.Serialization.Serializers.Int32Serializer::Deserialize