现有类    public class Student
    {
        public string id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
    }
反射的时候        public static object GetValueByIndex<TSource>(this TSource tSource, int index)
        {
            try
            {
                PropertyInfo[] pi = tSource.GetType().GetProperties();
                string FieldName = pi[index].Name;
                return tSource.GetType().GetProperty(FieldName).GetValue(tSource, null);
            }
            catch (IndexOutOfRangeException iore)
            {
                throw iore;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
这里的PropertyInfo[] pi = tSource.GetType().GetProperties();取出来之后的PropertyInfo是按照字母a-z的顺序,
就是 age,id,name的顺序
我想写个按照数字索引取值的扩展方法,就是取出来的PropertyInfo的顺序和我写实体时候的顺序是一样的,怎么做?

解决方案 »

  1.   

    将你的排序顺序写进数组传进去
      public static object GetValueByIndex<TSource>(this TSource tSource,string[] arr, int index)
      

  2.   

    还有就是为什么你要根据Index获取值
      

  3.   


    我就是有这个想法有的时候遍历可能会用到,比如
    for(var i=0;i........)
        var obj=xxxx.GetValueByIndex(i);
    之类的。
      

  4.   


    //反射出属性集合,取集合的count
    for (var i = 0; i < new XXXX().GetType().GetProperties().Count(); i++)
      

  5.   


    public class Student
    {
        private String[] fieldNames ;
        public Student()
        {
            PropertyInfo[] info = this.GetType().GetProperties();
            fieldNames = new String[info.Length];
            for (int i = 0; i < info.Length; i++)
            {
                fieldNames[0] = info[i].Name;
            }    }
        public string id { get; set; }
        public string name { get; set; }
        public int age { get; set; }    public Object getField(this Int32 index)
        {
            return fieldNames[index];
        }
    }
      

  6.   

     public Object getField(this Int32 index)
        {
            return fieldNames[index];
        }
    Object应该是String
      

  7.   

    还有,当你PropertyInfo[] info = this.GetType().GetProperties();
    的时候,字段已经被按照a-z排序了,所以你下边的循环不起任何作用。。
      

  8.   

    你这个用索引器吧public class Student
    {
        private String[] fieldNames ;
        public Student()
        {
            PropertyInfo[] info = this.GetType().GetProperties();
            fieldNames = new String[info.Length];
            for (int i = 0; i < info.Length; i++)
            {
                fieldNames[0] = info[i].Name;
            }    }
        public string id { get; set; }
        public string name { get; set; }
        public int age { get; set; }    public String this[Int32 index]
        {
            get
            {
                return fieldNames[index];
            }
        }
    }
      

  9.   

    我刚刚测试了下,是按照你写的顺序输出的        static void Main(string[] args)
            {
                Student _s = new Student()
                {
                    id = "1",
                    name = "bbs2241",
                    age = 25
                };            PropertyInfo[] _pArr = _s.GetType().GetProperties();
                foreach (var _p in _pArr)
                {
                    Console.WriteLine(_p.Name);
                }            Console.Read();
            }
        }    public class Student
        {
            public string id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
        }
      

  10.   

    我用VS2008 
    下面代码输出的顺序和类中属性的顺序是一样的eStudent stu = new Student();
                PropertyInfo[] info = stu.GetType().GetProperties();
                for (int i = 0; i < info.Length; i++)
                {
                    Response.Write(info[i].Name);
                }
      

  11.   

              Console.WriteLine(_s.GetValueByIndex(0));
                Console.WriteLine(_s.GetValueByIndex(1));
                Console.WriteLine(_s.GetValueByIndex(2));
    输出结果:1
    bbs2241
    25
    测试环境:vs2010,win2003 framework3.5
      

  12.   

    To bbs2241 and IHandler:我这个是在Silverlight+三层下做的,sl调用的是wcf,可能和这个有关,我在WebForm环境下也试过,是按照我的顺序输出的。你们有谁了解这个?为什么通过wcf之后就变样了呢?
      

  13.   

    LZ纯属无聊。Silverlght + WCF 的问题拿来这里发。Silverlght 中使用反射也基本上用不到LZ的需求, 这个需求纯属无聊。WCF 传到Silverlight后实体属性的排序会变,是因为WCF才不会有LZ这么怪异的需求呢,WCF是采用
    DataContract 和 DataMember 数据契约 进行序列化的,[DataMember(Order=n) ] 本身就可以
    指定属性在序列化/反序列化时的排序。
      

  14.   


    不是无聊。因为sl这个东西我本来也是边学边做,很多东西都不会,网上也很难查到,于是我就用自己的想法去实现。比如说,从DataGrid里取数据写到Excel里吧。我不可能直接把ItemSource写进去,因为ItemSource中实体字段的顺序就是a-z,绑定呈现的时候肯定不会去按照a-z的顺序绑定吧?所以我就想用这种方法让数据源的字段的顺序和我写实体时候的顺序一样(写实体的顺序和绑定的顺序是一样的),这样再往Excel里写多方便。。可能方法傻B了点。。但是我也不会别的方法啊,在网上查了一堆,基本上没有符合我需求的方法,所以就到这里来问问,如果有什么好的方法教教我,我就不用这么干了。
      

  15.   

    To sunpire:附:http://topic.csdn.net/u/20100727/13/331cc27e-ea12-416c-8aa2-48cb202724dc.html按照阿泰和如梦的思路,模板已经画好,就差往里填充数据。。
      

  16.   


    不明白, 绑定的时候的顺序和“定义实体时的属性顺序”、“ItemsSource数据源中的属性顺序”不是一点关系都没有的么?关于导出Excel,有一点看法:在出现了滚动条的情况下,只能从ItemsSource中去获取数据,从前台的DataGrid中获取不到在滚动条下方的数据的,所以,导出Excel应是根据 Header的模板 确定Excel各列的格式,其他的还是只能通过对后台ItemsSource调用反射取值,并应用IValueConverter, 这样,根本就用不到“属性的次序”。
      

  17.   

        public class Student
        {
            public string id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
        }
    难道你这个动态生成的不成?
    都是写好之后再调用的,有必要排序吗,里面有多少个,要哪个的值那还不是手到擒来
      

  18.   


    同意楼上,需要显示时,才有可能需要排序。否则,给属性排序是BT需求。
    public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); }
    public string GetClassName() { return TypeDescriptor.GetClassName(this, true); }
    public string GetComponentName() { return TypeDescriptor.GetComponentName(this, true); }
    public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); }
    public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); }
    public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); }
    public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); }
    public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); }
    public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); }