/// <summary> /// Deserilizes an instance from BSONDocument. /// If instance is not pre-allocated (result=null) tries to create a type /// decorated with BSONSerializableAttribute(guid) /// </summary> public IBSONDeserializable Deserialize(BSONDocument doc, IBSONDeserializable result = null) { object ctx = null; if (result == null) { var telm = doc[TypeIDFieldName] as BSONBinaryElement; if (telm == null) { throw new BSONException(StringConsts.BSON_DESERIALIZER_DOC_MISSING_TID_ERROR.Args(TypeIDFieldName)); } var tid = telm.Value.Data.AsGUID(Guid.Empty); if (tid == Guid.Empty) { throw new BSONException(StringConsts.BSON_DESERIALIZER_DOC_TID_GUID_ERROR.Args(TypeIDFieldName, telm.Value)); } try { var tresult = m_Resolver.Resolve(tid); result = SerializationUtils.MakeNewObjectInstance(tresult) as IBSONDeserializable; if (result == null) { throw new BSONException("result isnot IBSONDeserializable"); } } catch (Exception error) { throw new BSONException(StringConsts.BSON_DESERIALIZER_MAKE_TYPE_ERROR.Args(tid, error.ToMessageWithType()), error); } } try { result.DeserializeFromBSON(this, doc, ref ctx); } catch (Exception error) { throw new BSONException(StringConsts.BSON_DESERIALIZER_DFB_ERROR.Args(error.ToMessageWithType()), error); } return(result); }