/// <summary>
        /// MongoDB添加数据
        /// </summary>
        /// <typeparam name="T">类</typeparam>
        /// <param name="connstr">连接字符串如127.0.0.1</param>
        /// <param name="DatabaseName">数据库名</param>
        /// <param name="CollectionName">表名</param>
        /// <param name="t">添加的类</param>
        /// <returns>如果为空证明正确,不为空则为异常信息</returns>
        public static string WriteCollection<T>(String connstr, String DatabaseName, String CollectionName, T t)
        {
            String Message = string.Empty;
            try
            {
                using (Mongo_db = new MongoDBHelper(connstr, DatabaseName))
                {
                    if (t != null)
                    {
                        if (t.GetType().Name.IndexOf("List") >= 0)
                        {
                            //这里我自己写的判断传递进来的t是泛型集合的
                                 //问题就在于我如何把这T行转换有数据的List<T>型呢
                        }
                        else
                        {
                            
                            Mongo_db.Insert<T>(t, CollectionName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }
            return Message;
        }调用方法如:   List<M_Personal> modelList = new List<M_Personal>();
            Message = WriteDB.WriteCollection<List<M_Personal>;("127.0.0.1", "table1", "table3", modelList);
请高手们解答下,告诉我如何判断和转换,谢谢大神们,貌似要用到反射,我试了一些没有弄成我想要的。。泛型反射

解决方案 »

  1.   

    http://www.cnblogs.com/zhili/archive/2012/11/08/Generic_2.html 看看这个介绍
      

  2.   

    GetType()用到了,小弟对着方面用的少,了解的不多,希望大神指导下。。
      

  3.   

    谢谢你,我看了,网上也看了很多,能看下我需要的吗,我的T是List集合,能否告诉我怎么搞。。
      

  4.   


    public static string WriteCollection<T>(String connstr, String databaseName, String collectionName, IList<T> t)
    {
    String Message = string.Empty;
    try
    {
    using (Mongo_db = new MongoDBHelper(connstr, DatabaseName))
    {
    if (t != null)
    {
    Mongo_db.Insert<T>(t, CollectionName);
    }
    }
    }
    catch (Exception ex)
    {
    Message = ex.Message;
    }
    return Message;
    }// 调用
    List<M_Personal> modelList = new List<M_Personal>();
    Message = WriteDB.WriteCollection<M_Personal>;("127.0.0.1", "table1", "table3", modelList);
    这样写不知道行不行?
      

  5.   

    我现在已经  IList<T> obj = (IList<T>)AssemClass.CreateGeneric(typeof(List<>),typeof(T));
    动态创建泛型List<T>,难道还要动态创建T,然后找出T的所有值给T,然后再List<T>.add(T)?
      

  6.   

    这样写了的话,后台重载的方法不会区分是List<T>还是T,他都当T传了
      

  7.   

    那还要看你的List<T>中的T是否是固定的.如果不固定那就不好办!
      

  8.   

    http://bbs.csdn.net/topics/390512755 ,大侠,帮个忙吧!!!
      

  9.   

    不要乱用T,当你方法定义了一个T,那么方法内部所有的T都成了一样的了,但是你的泛型List的T还不固定,所以这种方法不合适!
      

  10.   

    这样写了的话,后台重载的方法不会区分是List<T>还是T,他都当T传了怎么会不行?(你的 Mongo_db.Insert<T> 方法可能也需要改一下)
    下面的测试正确通过:
    public static string WriteCollection<T>(String connstr, String databaseName, String collectionName, IList<T> t)
    {
    String message = string.Empty;
    try
    {
    var list = new List<T>();
    list.Insert(0, t[0]);
    Console.WriteLine(list.Count); // 输出1 list.Clear();
    list.AddRange(t);
    Console.WriteLine(list.Count); // 输出3 //using (Mongo_db = new MongoDBHelper(connstr, DatabaseName))
    //{
    // if (t != null)
    // {
    // Mongo_db.Insert<T>(t, CollectionName);
    // }
    //}
    }
    catch (Exception ex)
    {
    message = ex.Message;
    }
    return message;
    }static void Main(string[] args)
    {
    var list = new List<string>();
    list.Add("a");
    list.Add("b");
    list.Add("c"); WriteCollection(string.Empty, string.Empty, string.Empty, list); Console.ReadLine();
    }
      

  11.   

    这样写了的话,后台重载的方法不会区分是List<T>还是T,他都当T传了怎么会不行?(你的 Mongo_db.Insert<T> 方法可能也需要改一下)
    下面的测试正确通过:
    public static string WriteCollection<T>(String connstr, String databaseName, String collectionName, IList<T> t)
    {
    String message = string.Empty;
    try
    {
    var list = new List<T>();
    list.Insert(0, t[0]);
    Console.WriteLine(list.Count); // 输出1 list.Clear();
    list.AddRange(t);
    Console.WriteLine(list.Count); // 输出3 //using (Mongo_db = new MongoDBHelper(connstr, DatabaseName))
    //{
    // if (t != null)
    // {
    // Mongo_db.Insert<T>(t, CollectionName);
    // }
    //}
    }
    catch (Exception ex)
    {
    message = ex.Message;
    }
    return message;
    }static void Main(string[] args)
    {
    var list = new List<string>();
    list.Add("a");
    list.Add("b");
    list.Add("c"); WriteCollection(string.Empty, string.Empty, string.Empty, list); Console.ReadLine();
    }

    大侠,你这方法是对的,可以这么用,非常感谢你,分给你了!!!!