Source/NFX/Serialization/BSON/RowConverter.cs
/// <summary>
/// override to perform the conversion. the data is never null here, and ref cycles a ruled out
/// </summary>
override to perform the conversion. the data is never null here, and ref cycles a ruled out
override to perform the conversion . the data is never null here and ref cycles a ruled out
protected virtual BSONElement DoConvertCLRtoBSON(string name, object data, Type dataType, string targetName)
          {
            Func<string, object, BSONElement> func;
            if (m_CLRtoBSON.TryGetValue(dataType, out func)) return func(name, data);
            if (dataType.IsEnum)
              return name != null ? new BSONStringElement(name, data.ToString()) : new BSONStringElement(data.ToString());
            if (data is Row)
              return this.RowToBSONDocumentElement((Row)data, targetName, name: name);
            if (data is IDictionary)
            {
              var dict = (IDictionary)data;
              var result = new BSONDocument();
              foreach( var key in dict.Keys)
              {
                var fldName = key.ToString();
                var el = ConvertCLRtoBSON(fldName, dict[key], targetName);
                result.Set(el);
              }
              return name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result);
            }
            if (data is IEnumerable)
            {
              var list = (IEnumerable)data;
              List<BSONElement> elements = new List<BSONElement>();
              foreach( var obj in list)
              {
                var el = ConvertCLRtoBSON(null, obj, targetName);
                elements.Add(el);
              }
              var result = name != null ? new BSONArrayElement(name, elements.ToArray()) : new BSONArrayElement(elements.ToArray());
              return result;
            }
            throw new BSONException(StringConsts.CLR_BSON_CONVERSION_TYPE_NOT_SUPPORTED_ERROR.Args(dataType.FullName));
          }
protected virtual BSONElement DoConvertCLRtoBSON ( string name , object data , Type dataType , string targetName ) { Func < string , object , BSONElement > func ; if ( m_CLRtoBSON . TryGetValue ( dataType , out func ) ) return func ( name , data ) ; if ( dataType . IsEnum ) return name != null ? new BSONStringElement ( name , data . ToString ( ) ) : new BSONStringElement ( data . ToString ( ) ) ; if ( data is Row ) return this . RowToBSONDocumentElement ( ( Row ) data , targetName , name : name ) ; if ( data is IDictionary ) { var dict = ( IDictionary ) data ; var result = new BSONDocument ( ) ; foreach ( var key in dict . Keys ) { var fldName = key . ToString ( ) ; var el = ConvertCLRtoBSON ( fldName , dict [ key ] , targetName ) ; result . Set ( el ) ; } return name != null ? new BSONDocumentElement ( name , result ) : new BSONDocumentElement ( result ) ; } if ( data is IEnumerable ) { var list = ( IEnumerable ) data ; List < BSONElement > elements = new List < BSONElement > ( ) ; foreach ( var obj in list ) { var el = ConvertCLRtoBSON ( null , obj , targetName ) ; elements . Add ( el ) ; } var result = name != null ? new BSONArrayElement ( name , elements . ToArray ( ) ) : new BSONArrayElement ( elements . ToArray ( ) ) ; return result ; } throw new BSONException ( StringConsts . CLR_BSON_CONVERSION_TYPE_NOT_SUPPORTED_ERROR . Args ( dataType . FullName ) ) ; }
override to perform the conversion.
override to perform the conversion .
//1 Primitive/direct types //2 Enums //3 Complex Types //IDictionary //must be before IEnumerable //IEnumerable
name
string
data
object
dataType
Type
targetName
string