public static string getFieldNameWithoutPrefix(string fname)//从对象的属性名得出真实的对象属性名称,也就是去掉所有前缀的名称 { string FieldName; if (fname.Contains("_")) { int i = fname.LastIndexOf('_'); FieldName = fname.Substring(i + 1); if (fname.Contains("xl_") && (FieldName.EndsWith("List"))) { FieldName = FieldName.Substring(0, FieldName.Length - 4); } return(FieldName); } else { return(null); } }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { BsonReaderBookmark Bookmark = Reader.GetBookmark(); BsonType? DataTypeBak = DataType; if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Document: break; case BsonType.Boolean: return(Reader.ReadBoolean()); case BsonType.Int32: return(Reader.ReadInt32()); case BsonType.Int64: return(Reader.ReadInt64()); case BsonType.Decimal128: return((decimal)Reader.ReadDecimal128()); case BsonType.Double: return(Reader.ReadDouble()); case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: case BsonType.Symbol: case BsonType.JavaScript: case BsonType.JavaScriptWithScope: return(Reader.ReadString()); case BsonType.Binary: return(Reader.ReadBytes()); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Object or value expected."); } LinkedList <KeyValuePair <string, object> > Properties = new LinkedList <KeyValuePair <string, object> >(); LinkedList <KeyValuePair <string, object> > LowerCase = null; string TypeName = string.Empty; Guid ObjectId = Guid.Empty; string CollectionName = string.Empty; string FieldName; BsonType ValueType; object Value; Reader.ReadStartDocument(); while (Reader.State == BsonReaderState.Type) { ValueType = Reader.ReadBsonType(); if (ValueType == BsonType.EndOfDocument) { break; } FieldName = Reader.ReadName(); switch (ValueType) { case BsonType.Array: Value = GeneratedObjectSerializerBase.ReadArray(null, this.Provider, Reader, ValueType); break; case BsonType.Binary: Value = Reader.ReadBytes(); break; case BsonType.Boolean: Value = Reader.ReadBoolean(); break; case BsonType.DateTime: Value = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()); break; case BsonType.Decimal128: Value = (decimal)Reader.ReadDecimal128(); break; case BsonType.Document: Value = this.Deserialize(Reader, ValueType, true); break; case BsonType.Double: Value = Reader.ReadDouble(); break; case BsonType.Int32: Value = Reader.ReadInt32(); break; case BsonType.Int64: Value = Reader.ReadInt64(); break; case BsonType.JavaScript: Value = Reader.ReadJavaScript(); break; case BsonType.JavaScriptWithScope: Value = Reader.ReadJavaScriptWithScope(); break; case BsonType.Null: Value = null; Reader.ReadNull(); break; case BsonType.ObjectId: Value = Reader.ReadObjectId(); break; case BsonType.String: Value = Reader.ReadString(); break; case BsonType.Symbol: Value = Reader.ReadSymbol(); break; default: throw new Exception("Unrecognized data type: " + ValueType.ToString()); } switch (FieldName) { case "_id": if (Value is Guid Guid) { ObjectId = Guid; } else if (Value is string s) { ObjectId = new Guid(s); } else if (Value is byte[] A) { ObjectId = new Guid(A); } else if (Value is ObjectId ObjId) { ObjectId = GeneratedObjectSerializerBase.ObjectIdToGuid(ObjId); } else { throw new Exception("Unrecognized Object ID type: " + Value.GetType().FullName); } break; case "_type": TypeName = Value?.ToString(); if (this.returnTypedObjects && !string.IsNullOrEmpty(TypeName)) { Type DesiredType = Types.GetType(TypeName); if (DesiredType is null) { DesiredType = typeof(GenericObject); } if (DesiredType != typeof(GenericObject)) { IObjectSerializer Serializer2 = this.provider.GetObjectSerializer(DesiredType); Reader.ReturnToBookmark(Bookmark); return(Serializer2.Deserialize(Reader, DataTypeBak, Embedded)); } } break; case "_collection": CollectionName = Value?.ToString(); break; default: if (FieldName.EndsWith("_L")) { string s = FieldName.Substring(0, FieldName.Length - 2); bool Ignore = false; foreach (KeyValuePair <string, object> P in Properties) { if (P.Key == s) { Ignore = true; break; } } if (!Ignore) { if (LowerCase is null) { LowerCase = new LinkedList <KeyValuePair <string, object> >(); } LowerCase.AddLast(new KeyValuePair <string, object>(s, Value)); } } else { Properties.AddLast(new KeyValuePair <string, object>(FieldName, Value)); } break; } } if (!(LowerCase is null)) { foreach (KeyValuePair <string, object> P in LowerCase) { bool Ignore = false; foreach (KeyValuePair <string, object> P2 in Properties) { if (P2.Key == P.Key) { Ignore = true; break; } } if (!Ignore) { Properties.AddLast(new KeyValuePair <string, object>(P.Key + "_L", P.Value)); } } } Reader.ReadEndDocument(); return(new GenericObject(CollectionName, TypeName, ObjectId, Properties)); }