public string ConvertDateTimeToJsonStr(DateTime date) { try { //var bsonDocument = new BsonDocument(); var assembly = new StiDataAssemblyHelper("MongoDB.Bson.dll"); //var bsonElement = new BsonElement("date", BsonValue.Create(date)); var bsonValue = assembly.GetType("MongoDB.Bson.BsonValue").GetMethod("Create").Invoke(null, new object[] { date }); var bsonElement = assembly.CreateObject("MongoDB.Bson.BsonElement", "date", bsonValue); var str = bsonElement.ToString().Replace("date=", ""); return(string.Format("\"{0}\"", str)); } catch { return(null); } }
private string GetAsJson(object obj) { var assembly = new StiDataAssemblyHelper("MongoDB.Bson.dll"); var settings = assembly.CreateObject("MongoDB.Bson.IO.JsonWriterSettings"); settings.GetType().GetProperty("OutputMode").SetValue(settings, 0, null); using (var writer = new StringWriter()) { //var writer2 = BsonWriter.Create(writer, settings as JsonWriterSettings); var method = assembly.GetType("MongoDB.Bson.IO.BsonWriter").GetMethod("Create", new[] { writer.GetType(), settings.GetType() }); using (var writer2 = method.Invoke(null, new[] { writer, settings }) as IDisposable) { var bsonWriterType = assembly.GetType("MongoDB.Bson.IO.BsonWriter"); //BsonSerializer.Serialize(writer2, typeof(object), obj); method = assembly.GetType("MongoDB.Bson.Serialization.BsonSerializer").GetMethod("Serialize", new[] { bsonWriterType, typeof(Type), obj.GetType() }); method.Invoke(null, new[] { writer2, typeof(object), obj }); return(writer.ToString()); } } }