public class sort<T>
    {
        private string m_colName;//<T>排序属性名
        public sort(string colName)
        {
            m_colName = colName;
        }
       //用来给List<T>排序
        public static int CompareCols(T x, T y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (y == null)
                {
                    return 1;
                }
                else
                {
                    return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误 17 “T”并不包含“m_colName”的定义                }
            }
        }
    }有什么好的方法吗??

解决方案 »

  1.   

    public class sort<T>
        {
            private T m_colName;//<T>排序属性名
            public sort(T colName)
            {
                m_colName = colName;
            }
      

  2.   

    to ojekleen(三尾) 这样写是不行的,我是要取<T>里的属性to hdt(倦怠) 能给点代码吗??反射是可行
      

  3.   

    ojekleen(三尾) ( ) 信誉:100  2007-09-24 09:50:04  得分: 0  
     
     
       public class sort<T>
        {
            private T m_colName;//<T>排序属性名
            public sort(T colName)
            {
                m_colName = colName;
            }  
     -----------------------------------------------------
    正解
      

  4.   

    interface ia {
            string col_name { get; set;}
        }
        class myc<T>   where T:ia
        {
            void youmethod( T x )
            {
                x.col_name = ...
     
            }
        }
      

  5.   

    楼上的,我要根据 <T>里的属性 
          return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误 17 “T”并不包含“m_colName”的定义
      

  6.   

    public static int CompareCols(T x, T y)
    这里T的类型未知,所以出错。排序的关键在于比较,所以要求被排序的类要实现IComparable接口如:class Racer:IComparable
        {
            private string _name;        public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private int _age;        public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
            public Racer(string name,int age)
            {
                this._name = name;
                this._age = age;
            }        #region IComparable 成员        public int CompareTo(object obj)
            {
                Racer temp = (Racer)obj;            return this._name.CompareTo(temp._name);
            }        #endregion
        }
    排序时用:
     List<Racer> racers = new List<Racer>();            racers.Add(new Racer("aaaa",12));
                racers.Add(new Racer("ddddd",15));
                racers.Add(new Racer("bbb",3));            racers.Sort();            dataGridView1.DataSource = racers;http://bbs.gogolike.com/showtopic-476.aspx
      

  7.   

    另外,如果说通用泛型排序,除了反射(这个方法是不是真的具有实用性还值得探讨),在vs2008中,可以用linq语法查询进行,这种方法应当可以说是非常好的解决方法。
      

  8.   

    to lnwuyaowei(风可以追我) 我想实现一个可以根据传入属性名对List<T>排序的公用类
      

  9.   

    除非你知道这个传入的东西是什么,否则是不能实现的,如果对一个未知元素可以在泛型中排序,微软早就实现这个功能了。一个可以选择的方法是:定义一个接口,接口中定义了排序字段,其它的类实现这个接口,在sort类中对接口排序。这样做,其实也是建立在你清楚要对什么排序的基础之上。
      

  10.   

    to hdt(倦怠)
       interface ia {
            string col_name { get; set;}
        }
    col_name是确定的吗,我要实现的是动态的
      

  11.   

    public sort(string colName)
            {
                m_colName = colName;
            }
    根据传入的属性名对List<T>排序
      

  12.   

    如果要使用自定义属性进行比较,就要让传入的类实现某个接口。
    public interface IMyClass
    {
    m_colName
    {
    get;
    set;
    }
    }public class sort<T>
    {
    public static int CompareCols(T x, T y)
    where T :IMyClass
    {
    // 你的方法
    }
    }// 可传入的类
    public class MyClass : IMyClass
    {
    private string m_colname;
    public string m_colName
    {
    get {return m_colname;}
    set {m_colname=value;}
    }
    }
      

  13.   

    to ezhuyin(碧海蓝天) 可能我说的还不是很清楚
    MyClass是动态的,排序属性也是动态的
    我要根据MyClass不同属性排序,按照你这种做法,我只能根据m_colName对MyClass进行排序了
      

  14.   

    FieldInfo[] fields = typeof(T).GetFields();
      

  15.   

    to JerrySabrina(等候莹莹) 
    属性名称是通过构造函数public sort(string colName)传入的
    问题是return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误17“T”并不包含“m_colName”的定义
      

  16.   

    .net2005里TreeView怎么绑定? 
    private void InitTreeView()
        {
                for (int i=0;i<GetBigType ().Rows .Count ;i ++)
                {
    DataTable dt=GetBigType();
                      TreeNode node = new TreeNode();
                      node.Expanded = false;//设置节点是否展开,取反
                    //look this code run 
                    node.Text =dt.rows[i]["bigname"].ToString ();
                    node.Value = dt.Rows[i]["TradeBigTypeid"].ToString();
                    this.MyTreeView.Nodes.Add(node);
                            
                }           
        }这是一级绑定到节点里.
       //一级节点
         public DataTable  GetBigType() 
         {         
             using(SqlConnection con=new SqlConnection (MCommon.ConnectString ))
             {
                 con.Open ();
                 string sql="select * from TradeBigType where status=1";
                SqlCommand  cmd=new SqlCommand (sql,con);
                 SqlDataAdapter ada=new SqlDataAdapter (cmd);
                 DataTable dt=new DataTable ();
                 ada.Fill (dt);
                return dt;
             }
         }
         //二级节点    public DataTable GetMidType(string bigname)
        {
            using (SqlConnection con = new SqlConnection(MCommon.ConnectString))
            {
                con.Open();
                string sql = "select * from trademidtype where bigtradeid=(select tradebigtypeid from tradebigtype where bigname='" + bigname + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);
                return dt;
            }
        }    //三级节点
        public DataTable GetSmallType(string bigname,string midname)
        {
            using (SqlConnection con = new SqlConnection(MCommon.ConnectString))
            {
                con.Open();
                string sql = "select * from TradeSmallType where bigid=(select tradebigtypeid from tradebigtype where bigname='" + bigname + "') and midid=(select trademidtypeid from trademidtype where midname='" + midname + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);
                return dt;
            }
        }这是我用到的得到数据的方法  都要传参数的,但是我不知道怎么把他们加到树里面的二级三级里去了各位路过的高手,能不能就这我这几个方法把二级 三级绑到树里去,小女子在这里先谢谢各位了
      

  17.   

    public static int CompareCols(sort1<T> x, sort1<T> y)
      

  18.   

    public static int CompareCols(sort<T> x, sort<T> y)应该是sort,我为了避免和默认sort冲突才加了1,不好意思
      

  19.   

    比较晕,我的帖子也太受欢迎,gaolingli() ( ) 欢迎你的到来
      

  20.   

    to zhuqueta0101() 你的代码呢?
      

  21.   

    public static int CompareCols(sort<T> x, sort<T> y)其他都不变,还是按楼主的,就是函数参数改变了,发现了没
      

  22.   

    不行
    List<T>.Sort()没有public static int CompareCols(sort<T> x, sort<T> y)这个重载的方法
      

  23.   

    看看你list在哪定义的,怎么调用的我的意思是
    public class sort<T>
    {
    private string m_colName;//<T>排序属性名
    public sort(string colName)
    {
    m_colName = colName;
    }
    //用来给List<T>排序
    public static int CompareCols(sort<T> x, sort<T> y)这个重载的方法{
    if (x == null)
    {
    if (y == null)
    {
    return 0;
    }
    else
    {
    return -1;
    }
    }
    else
    {
    if (y == null)
    {
    return 1;
    }
    else
    {
    return x.m_colName.CompareTo(y.m_colName);//这句话出了问题错误17“T”并不包含“m_colName”的定义}
    }
    }
    }
      

  24.   

    如果要动态的,那就用反射。或者,可以定义一个接口
    public interface IPropertyComparable{
        IComparable GetComparableProperty(string propertyName);
    }
    根据给定的属性名返回可以比较的值,可以不用反射。
      

  25.   

    我是这样调用的,
    MyList.Sort(sort<Model.CHChance>("Name").CompareCols);//根据属性名Name给MyList排序
      

  26.   

    to danjiewu(阿丹) 
    能不能详细一点?给点代码说明
      

  27.   

    奇怪,我有几个问题一个是
    public class sort<T>为什么不是继承IComparer<T>的呢?
    public class sort<T>:IComparer<T>?然后就应该是实现接口
    public int Compare(T x, T y)才对吧?然后你的m_colName是谁的私有变量呢?T的?还是sort类的?要是T的,为什么你定义在sort类下面呢?
    要是sort的,x.m_colName又怎么理解呢?
      

  28.   

    使用类型参数的约束 对T进行说明public class Test
    {
      public string m_colName;
    }public class sort<T> where T : Test
    {
       .......
    }
      

  29.   

    public class sort<T> where T : IPropertyComparable
        {
            private string m_colName;//<T>排序属性名
            public sort(string colName)
            {
                m_colName = colName;
            }
           //用来给List<T>排序
            public static int CompareCols(T x, T y)
            {
                if (x == null)
                {
                    if (y == null)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    if (y == null)
                    {
                        return 1;
                    }
                    else
                    {
                        return x.GetComparableProperty("m_colName").CompareTo(y.GetComparableProperty("m_colName"));                }
                }
            }
        }public class CHChance:IPropertyComparable{
        public IComparable GetComparableProperty(string propertyName){
             switch(propertyName){
                 case "m_colName": return m_colName;
                 case "m_colName2": 
                 ...;
                 default: return (IComparable) this.GetType().GetProperty(propertyName).GetValue(this,null);
             
        }    private string m_col;
        public string m_colName{
            get{return m_col;}
            set{ m_col = value;}    }
    }
      

  30.   

    danjiewu(阿丹) very Good完全可以实现我的功能但是我每个实体类都要实现IPropertyComparable接口,当实体类属性比较多,代码量有点大,当实体类增加属性时,要
    修改GetComparableProperty方法,灵活性不是太好,有没有更好的idea
      

  31.   

    default: return (IComparable) this.GetType().GetProperty(propertyName).GetValue(this,null);这个是用反射的,能满足你的要求。如果对效率要求不是很高,就用反射好了。最好是定义一个实现了IPropertyComparable的基类,基类里的GetComparableProperty方法用反射实现。子类可以用基类的GetComparableProperty方法,也可以重写提高效率。
      

  32.   

    定义一个包含m_colName属性的基类或接口(暂命名为H),然后在将源码里将所有用到的T替换为H,这样要比你用范型类好。
      

  33.   


    多谢大家!!!~~~
    多谢 danjiewu(阿丹)强力支援