コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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());
                }
            }
        }
コード例 #3
0
        public override DataTable GetDataTable(string collectionName, string query, int?index = null, int?count = null)
        {
            try
            {
                var database = GetDatabase();
                //var collection = database.GetCollection<BsonDocument>(collectionName);
                var method = database.GetType().GetMethods().FirstOrDefault(m => m.Name == "GetCollection" &&
                                                                            !m.IsGenericMethodDefinition &&
                                                                            m.GetParameters().Length == 1 &&
                                                                            m.GetParameters()[0].ParameterType ==
                                                                            typeof(string));

                object collection = null;
                if (method != null)//net40
                {
                    collection = method.Invoke(database, new object[] { collectionName });
                }

                /*else//net45
                 * {
                 *  var settingsType = AssemblyHelper.GetType("MongoDB.Driver.MongoCollectionSettings");
                 *  method = database.GetType().GetMethod("GetCollection", new[] { typeof (string), settingsType });
                 *  method = method.MakeGenericMethod(database.GetType());
                 *  collection = method.Invoke(database, new object[] { collectionName, null });
                 * }*/

                //var cursor = string.IsNullOrWhiteSpace(query) ? collection.FindAll() : collection.Find(new QueryDocument(BsonDocument.Parse(query)));
                var         assembly = new StiDataAssemblyHelper("MongoDB.Bson.dll");
                IEnumerable cursor   = null;
                if (string.IsNullOrWhiteSpace(query))
                {
                    method = collection.GetType().GetMethod("FindAll");
                    cursor = method.Invoke(collection, null) as IEnumerable;
                }
                else
                {
                    var bsonDocument = assembly.GetType("MongoDB.Bson.BsonDocument")
                                       .GetMethod("Parse")
                                       .Invoke(null, new object[] { query });
                    var queryDocument = AssemblyHelper.CreateObject("MongoDB.Driver.QueryDocument", bsonDocument);

                    cursor = collection.GetType().GetMethod("Find").Invoke(collection, new[] { queryDocument }) as IEnumerable;
                }

                var list = new List <JToken>();
                var i    = 0;
                foreach (var row in cursor)
                {
                    if (count != null && i >= count)
                    {
                        break;
                    }
                    if (index == null || i >= index)
                    {
                        try
                        {
                            //var jStr = row.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict });

                            //var jsonWriterSettings = assembly.CreateObject("MongoDB.Bson.IO.JsonWriterSettings");
                            //jsonWriterSettings.GetType().GetProperty("OutputMode").SetValue(jsonWriterSettings, 0, null);

                            //var method2 = assembly.GetType("MongoDB.Bson.BsonExtensionMethods").GetMethod("ToJson", new[] {typeof(object), typeof(Type), jsonWriterSettings.GetType() });
                            //var jStr = method2.Invoke(row, new[] { jsonWriterSettings }) as string;
                            var jStr = GetAsJson(row);

                            var jToken = JToken.Parse(jStr);
                            list.Add(jToken);
                        }
                        catch
                        {
                        }
                    }

                    i++;
                }

                var dataSet = StiJsonToDataSetConverter.GetDataSet(list, true);
                return(dataSet != null && dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null);
            }
            catch (Exception exception)
            {
                if (exception.InnerException != null)
                {
                    throw exception.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }