当前位置: 首页>>代码示例>>C#>>正文


C# BsonWriter.WriteName方法代码示例

本文整理汇总了C#中MongoDB.Bson.IO.BsonWriter.WriteName方法的典型用法代码示例。如果您正苦于以下问题:C# BsonWriter.WriteName方法的具体用法?C# BsonWriter.WriteName怎么用?C# BsonWriter.WriteName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoDB.Bson.IO.BsonWriter的用法示例。


在下文中一共展示了BsonWriter.WriteName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Serialize

        public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            var method = (MethodInfo)value;

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName("Type");
            bsonWriter.WriteString(method.DeclaringType.AssemblyQualifiedName);
            bsonWriter.WriteName("Method");
            bsonWriter.WriteString(GetMethodSignature(method));

            bsonWriter.WriteEndDocument();

        }
开发者ID:LenFon,项目名称:Bifrost,代码行数:13,代码来源:MethodInfoSerializer.cs

示例2: Serialize

        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            IDictionary<string, object> obj = value as IDictionary<string, object>;
            if (obj == null)
            {
                bsonWriter.WriteNull();
                return;
            }

            bsonWriter.WriteStartDocument();
            foreach (var member in obj)
            {
                bsonWriter.WriteName(member.Key);
                object memberValue = member.Value;

                if (memberValue == null)
                {
                    bsonWriter.WriteNull();
                }
                else
                {
                    nominalType = memberValue.GetType();
                    var serializer = BsonSerializer.LookupSerializer(nominalType);
                    serializer.Serialize(bsonWriter, nominalType, memberValue, options);
                }
            }
            bsonWriter.WriteEndDocument();
        }
开发者ID:abel,项目名称:sinan,代码行数:28,代码来源:VariantBsonSerializer.cs

示例3: Serialize

        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
                return;
            }
            var metaObject = ((IDynamicMetaObjectProvider)value).GetMetaObject(Expression.Constant(value));
            var memberNames = metaObject.GetDynamicMemberNames().ToList();
            if (memberNames.Count == 0)
            {
                bsonWriter.WriteNull();
                return;
            }

            bsonWriter.WriteStartDocument();
            foreach (var memberName in memberNames)
            {
                bsonWriter.WriteName(memberName);
                var memberValue = BinderHelper.GetMemberValue(value, memberName);
                if (memberValue == null)
                    bsonWriter.WriteNull();
                else
                {
                    var memberType = memberValue.GetType();
                    var serializer = BsonSerializer.LookupSerializer(memberType);
                    serializer.Serialize(bsonWriter, memberType, memberValue, options);
                }
            }
            bsonWriter.WriteEndDocument();
        }
开发者ID:BraveNewMath,项目名称:Simple.Data.MongoDB,代码行数:31,代码来源:DynamicBsonSerializer.cs

示例4: WriteNullableDouble

        protected void WriteNullableDouble(BsonWriter writer,string name,double? value)
        {
            writer.WriteName(name);

            if ( value != null )
            {
                writer.WriteDouble(value.Value);
            }
            else
            {
                writer.WriteNull();
            }
        }
开发者ID:BernhardGlueck,项目名称:Phare,代码行数:13,代码来源:MongoDbSerializerBase.cs

示例5: Serialize

        public override void Serialize( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options )
        {
            var dictionary = (DynamicDictionary)value;

            bsonWriter.WriteStartDocument();

            bsonWriter.WriteString( "_t", "DynamicDictionary" );

            if( dictionary != null )
            {
                foreach( var entry in dictionary )
                {
                    bsonWriter.WriteName( entry.Key.Replace( '.', '\x03' ) );
                    BsonSerializer.Serialize( bsonWriter, typeof( object ), entry.Value );
                }
            }

            bsonWriter.WriteEndDocument();
        }
开发者ID:RabidArts,项目名称:TrivialMongoMessageQueue,代码行数:19,代码来源:DynamicDictionarySerializer.cs

示例6: SerializeExtraElements

 private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
 {
     var extraElements = extraElementsMemberMap.Getter(obj);
     if (extraElements != null)
     {
         if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
         {
             var bsonDocument = (BsonDocument)extraElements;
             foreach (var element in bsonDocument)
             {
                 bsonWriter.WriteName(element.Name);
                 BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null);
             }
         }
         else
         {
             var dictionary = (IDictionary<string, object>)extraElements;
             foreach (var key in dictionary.Keys)
             {
                 bsonWriter.WriteName(key);
                 var value = dictionary[key];
                 if (value == null)
                 {
                     bsonWriter.WriteNull();
                 }
                 else
                 {
                     var bsonValue = BsonTypeMapper.MapToBsonValue(dictionary[key]);
                     BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), bsonValue, null);
                 }
             }
         }
     }
 }
开发者ID:robinNode,项目名称:mongo-csharp-driver,代码行数:34,代码来源:BsonClassMapSerializer.cs

示例7: SerializeMember

        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            if (value == null && nominalType.IsInterface)
            {
                bsonWriter.WriteNull();
            }
            else if (value == null && memberMap.MemberTypeIsBsonValue)
            {
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteBoolean("_csharpnull", true);
                bsonWriter.WriteEndDocument();
            }
            else
            {
                var actualType = (value == null) ? nominalType : value.GetType();
                var serializer = memberMap.GetSerializer(actualType);
                serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
            }
        }
开发者ID:robinNode,项目名称:mongo-csharp-driver,代码行数:28,代码来源:BsonClassMapSerializer.cs

示例8: Serialize

 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         bsonWriter.WriteStartArray();
         int index = 0;
         foreach (var element in (IEnumerable) value) {
             bsonWriter.WriteName(index.ToString());
             BsonSerializer.Serialize(bsonWriter, typeof(object), element);
             index++;
         }
         bsonWriter.WriteEndArray();
     }
 }
开发者ID:kenegozi,项目名称:mongo-csharp-driver,代码行数:20,代码来源:CollectionSerializers.cs

示例9: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                // Nullable types are weird because they get boxed as their underlying value type
                // we can best handle that by switching the nominalType to the underlying value type
                // (so VerifyNominalType doesn't fail and we don't get an unnecessary discriminator)
                if (nominalType.IsGenericType && nominalType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    nominalType = nominalType.GetGenericArguments()[0];
                }

                VerifyNominalType(nominalType);
                var actualType = (value == null) ? nominalType : value.GetType();
                if (actualType != _classMap.ClassType)
                {
                    var message = string.Format("BsonClassMapSerializer.Serialize for type {0} was called with actualType {1}.",
                        BsonUtils.GetFriendlyTypeName(_classMap.ClassType), BsonUtils.GetFriendlyTypeName(actualType));
                    throw new BsonSerializationException(message);
                }

                var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
                if (documentSerializationOptions == null)
                {
                    var message = string.Format(
                        "Serializer BsonClassMapSerializer expected serialization options of type {0}, not {1}.",
                        BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
                        BsonUtils.GetFriendlyTypeName(options.GetType()));
                    throw new BsonSerializationException(message);
                }

                bsonWriter.WriteStartDocument();
                BsonMemberMap idMemberMap = null;
                if (documentSerializationOptions.SerializeIdFirst)
                {
                    idMemberMap = _classMap.IdMemberMap;
                    if (idMemberMap != null)
                    {
                        SerializeMember(bsonWriter, value, idMemberMap);
                    }
                }

                if (actualType != nominalType || _classMap.DiscriminatorIsRequired || _classMap.HasRootClass)
                {
                    // never write out a discriminator for an anonymous class
                    if (!_classMap.IsAnonymous)
                    {
                        var discriminatorConvention = _classMap.GetDiscriminatorConvention();
                        var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
                        if (discriminator != null)
                        {
                            bsonWriter.WriteName(discriminatorConvention.ElementName);
                            BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), discriminator, null);
                        }
                    }
                }

                var allMemberMaps = _classMap.AllMemberMaps;
                var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;

                for (var memberMapIndex = 0; memberMapIndex < allMemberMaps.Count; ++memberMapIndex)
                {
                    var memberMap = allMemberMaps[memberMapIndex];
                    // note: if serializeIdFirst is false then idMemberMap will be null (so no property will be skipped)
                    if (memberMap != idMemberMap)
                    {
                        if (memberMapIndex != extraElementsMemberMapIndex)
                        {
                            SerializeMember(bsonWriter, value, memberMap);
                        }
                        else
                        {
                            SerializeExtraElements(bsonWriter, value, memberMap);
                        }
                    }
                }
                bsonWriter.WriteEndDocument();
            }
        }
开发者ID:robinNode,项目名称:mongo-csharp-driver,代码行数:94,代码来源:BsonClassMapSerializer.cs

示例10: SerializeMember

        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            var actualType = (value == null) ? nominalType : value.GetType();
            var serializer = memberMap.GetSerializer(actualType);
            serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
        }
开发者ID:purplecow,项目名称:mongo-csharp-driver,代码行数:15,代码来源:BsonClassMapSerializer.cs

示例11: OnSerialized

 protected override void OnSerialized(BsonWriter bsonWriter, object value, IBsonSerializationOptions options)
 {
     if (_migrations.Any())
     {
         bsonWriter.WriteName(VERSION_ELEMENT_NAME);
         var currentVersion = _versionDetectionStrategy.GetCurrentVersion();
         _versionSerializer.Serialize(bsonWriter, typeof(Version), currentVersion, null);
     }
 }
开发者ID:RasmusTG,项目名称:mongo-csharp-migrations,代码行数:9,代码来源:BsonMigrationSerializer.cs

示例12: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options
        ) {
            if (value == null) {
                bsonWriter.WriteNull();
            } else {
                var dictionary = (IDictionary) value;

                var representationOptions = options as RepresentationSerializationOptions;
                BsonType representation;
                if (representationOptions == null) {
                    representation = BsonType.Document;
                    foreach (object key in dictionary.Keys) {
                        var name = key as string; // check for null and type string at the same time
                        if (name == null || name.StartsWith("$") || name.Contains(".")) {
                            representation = BsonType.Array;
                            break;
                        }
                    }
                } else {
                    representation = representationOptions.Representation;
                }

                switch (representation) {
                    case BsonType.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry entry in dictionary) {
                            bsonWriter.WriteName((string) entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case BsonType.Array:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary) {
                            bsonWriter.WriteStartArray();
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid representation for type IDictionary.", representation);
                        throw new BsonSerializationException(message);
                }
            }
        }
开发者ID:kamiff,项目名称:mongo-csharp-driver,代码行数:58,代码来源:DictionarySerializer.cs

示例13: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // could get here with a BsonDocumentWrapper from BsonValueSerializer switch statement
            var wrapper = value as BsonDocumentWrapper;
            if (wrapper != null)
            {
                BsonDocumentWrapperSerializer.Instance.Serialize(bsonWriter, nominalType, value, null);
                return;
            }

            var bsonDocument = (BsonDocument)value;
            var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
            if (documentSerializationOptions == null)
            {
                var message = string.Format(
                    "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
                    BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
                    BsonUtils.GetFriendlyTypeName(options.GetType()));
                throw new BsonSerializationException(message);
            }

            bsonWriter.WriteStartDocument();
            BsonElement idElement = null;
            if (documentSerializationOptions.SerializeIdFirst && bsonDocument.TryGetElement("_id", out idElement))
            {
                bsonWriter.WriteName(idElement.Name);
                BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), idElement.Value, null);
            }

            foreach (var element in bsonDocument)
            {
                // if serializeIdFirst is false then idElement will be null and no elements will be skipped
                if (!object.ReferenceEquals(element, idElement))
                {
                    bsonWriter.WriteName(element.Name);
                    BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null);
                }
            }

            bsonWriter.WriteEndDocument();
        }
开发者ID:neo2018,项目名称:mongo-csharp-driver,代码行数:57,代码来源:BsonDocumentSerializer.cs

示例14: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var dictionary = (IDictionary)value;

                if (nominalType == typeof(object))
                {
                    var actualType = value.GetType();
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", BsonClassMap.GetTypeNameDiscriminator(actualType));
                    bsonWriter.WriteName("_v");
                    Serialize(bsonWriter, actualType, value, options); // recursive call replacing nominalType with actualType
                    bsonWriter.WriteEndDocument();
                    return;
                }

                var dictionaryOptions = options as DictionarySerializationOptions;
                if (dictionaryOptions == null)
                {
                    // support RepresentationSerializationOptions for backward compatibility
                    var representationOptions = options as RepresentationSerializationOptions;
                    if (representationOptions != null)
                    {
                        switch (representationOptions.Representation)
                        {
                            case BsonType.Array:
                                dictionaryOptions = DictionarySerializationOptions.ArrayOfArrays;
                                break;
                            case BsonType.Document:
                                dictionaryOptions = DictionarySerializationOptions.Document;
                                break;
                            default:
                                var message = string.Format("BsonType {0} is not a valid representation for a Dictionary.", representationOptions.Representation);
                                throw new BsonSerializationException(message);
                        }
                    }

                    if (dictionaryOptions == null)
                    {
                        dictionaryOptions = DictionarySerializationOptions.Defaults;
                    }
                }

                var representation = dictionaryOptions.Representation;
                if (representation == DictionaryRepresentation.Dynamic)
                {
                    representation = DictionaryRepresentation.Document;
                    foreach (object key in dictionary.Keys)
                    {
                        var name = key as string; // check for null and type string at the same time
                        if (name == null || name.StartsWith("$") || name.Contains("."))
                        {
                            representation = DictionaryRepresentation.ArrayOfArrays;
                            break;
                        }
                    }
                }

                switch (representation)
                {
                    case DictionaryRepresentation.Document:
                        bsonWriter.WriteStartDocument();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteName((string)entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                        }
                        bsonWriter.WriteEndDocument();
                        break;
                    case DictionaryRepresentation.ArrayOfArrays:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartArray();
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Value);
                            bsonWriter.WriteEndArray();
                        }
                        bsonWriter.WriteEndArray();
                        break;
                    case DictionaryRepresentation.ArrayOfDocuments:
                        bsonWriter.WriteStartArray();
                        foreach (DictionaryEntry entry in dictionary)
                        {
                            bsonWriter.WriteStartDocument();
                            bsonWriter.WriteName("k");
                            BsonSerializer.Serialize(bsonWriter, typeof(object), entry.Key);
                            bsonWriter.WriteName("v");
//.........这里部分代码省略.........
开发者ID:kamaradclimber,项目名称:mongo-csharp-driver,代码行数:101,代码来源:DictionarySerializer.cs

示例15:

 void IBsonSerializable.Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("$ref", _collectionName);
     bsonWriter.WriteName("$id");
     _id.WriteTo(bsonWriter);
     if (_databaseName != null)
     {
         bsonWriter.WriteString("$db", _databaseName);
     }
     bsonWriter.WriteEndDocument();
 }
开发者ID:moonreplace,项目名称:mongo-csharp-driver,代码行数:12,代码来源:MongoDBRef.cs


注:本文中的MongoDB.Bson.IO.BsonWriter.WriteName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。