现有类 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的顺序和我写实体时候的顺序是一样的,怎么做?
{
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的顺序和我写实体时候的顺序是一样的,怎么做?
public static object GetValueByIndex<TSource>(this TSource tSource,string[] arr, int index)
我就是有这个想法有的时候遍历可能会用到,比如
for(var i=0;i........)
var obj=xxxx.GetValueByIndex(i);
之类的。
//反射出属性集合,取集合的count
for (var i = 0; i < new XXXX().GetType().GetProperties().Count(); i++)
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];
}
}
{
return fieldNames[index];
}
Object应该是String
的时候,字段已经被按照a-z排序了,所以你下边的循环不起任何作用。。
{
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];
}
}
}
{
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; }
}
下面代码输出的顺序和类中属性的顺序是一样的eStudent stu = new Student();
PropertyInfo[] info = stu.GetType().GetProperties();
for (int i = 0; i < info.Length; i++)
{
Response.Write(info[i].Name);
}
Console.WriteLine(_s.GetValueByIndex(1));
Console.WriteLine(_s.GetValueByIndex(2));
输出结果:1
bbs2241
25
测试环境:vs2010,win2003 framework3.5
DataContract 和 DataMember 数据契约 进行序列化的,[DataMember(Order=n) ] 本身就可以
指定属性在序列化/反序列化时的排序。
不是无聊。因为sl这个东西我本来也是边学边做,很多东西都不会,网上也很难查到,于是我就用自己的想法去实现。比如说,从DataGrid里取数据写到Excel里吧。我不可能直接把ItemSource写进去,因为ItemSource中实体字段的顺序就是a-z,绑定呈现的时候肯定不会去按照a-z的顺序绑定吧?所以我就想用这种方法让数据源的字段的顺序和我写实体时候的顺序一样(写实体的顺序和绑定的顺序是一样的),这样再往Excel里写多方便。。可能方法傻B了点。。但是我也不会别的方法啊,在网上查了一堆,基本上没有符合我需求的方法,所以就到这里来问问,如果有什么好的方法教教我,我就不用这么干了。
不明白, 绑定的时候的顺序和“定义实体时的属性顺序”、“ItemsSource数据源中的属性顺序”不是一点关系都没有的么?关于导出Excel,有一点看法:在出现了滚动条的情况下,只能从ItemsSource中去获取数据,从前台的DataGrid中获取不到在滚动条下方的数据的,所以,导出Excel应是根据 Header的模板 确定Excel各列的格式,其他的还是只能通过对后台ItemsSource调用反射取值,并应用IValueConverter, 这样,根本就用不到“属性的次序”。
{
public string id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
难道你这个动态生成的不成?
都是写好之后再调用的,有必要排序吗,里面有多少个,要哪个的值那还不是手到擒来
同意楼上,需要显示时,才有可能需要排序。否则,给属性排序是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); }