private BsonValue Compose(object value) { var type = Codec.GetDataType(value, null); switch (type) { case DataType.Void: // nothing to do; return(BsonNull.Value); case DataType.String: return(new BsonString((string)value)); case DataType.Resource: case DataType.DistributedResource: return(new BsonDocument { { "type", 0 }, { "link", (value as IResource).Instance.Link } }); //return new BsonObjectId(new ObjectId((string)(value as IResource).Instance.Attributes["objectId"])); case DataType.Structure: return(ComposeStructure((Structure)value)); case DataType.VarArray: return(ComposeVarArray((object[])value)); case DataType.ResourceArray: if (value is IResource[]) { return(ComposeResourceArray((IResource[])value)); } else { return(ComposeResourceArray((IResource[])DC.CastConvert(value, typeof(IResource[])))); } case DataType.StructureArray: return(ComposeStructureArray((Structure[])value)); default: return(BsonValue.Create(value)); } }
/// <summary> /// Load property value (used by stores) /// </summary> /// <param name="name">Property name</param> /// <param name="age">Property age</param> /// <param name="value">Property value</param> /// <returns></returns> public bool LoadProperty(string name, ulong age, DateTime modificationDate, object value) { var pt = template.GetPropertyTemplate(name); if (pt == null) { return(false); } #if NETSTANDARD1_5 var pi = resource.GetType().GetTypeInfo().GetProperty(name); #else var pi = resource.GetType().GetProperty(pt.Name); #endif if (pi.PropertyType == typeof(DistributedPropertyContext)) { return(false); } try { if (pi.CanWrite) { pi.SetValue(resource, DC.CastConvert(value, pi.PropertyType)); } } catch (Exception ex) { //Console.WriteLine(resource.ToString() + " " + name); Global.Log(ex); } SetAge(pt.Index, age); SetModificationDate(pt.Index, modificationDate); return(true); }