我有一个简单的问题,但我就可以了,因为今天早上...初始化列表<T>从GridView的数据源与反射的类型
我怎么能初始化一个List<T>
,而不知道它的类型?或者(可能更简单)我可以在GridView.DataSource
上做OrderBy
吗?
这里我的代码,在这一刻:
Type type = (from asm in AppDomain.CurrentDomain.GetAssemblies()
from item in asm.GetTypes()
where item.IsClass && item.Name.Equals(myType)
select item).Single();
PropertyInfo property = type.GetProperty(myProperty);
this.DataSource = ((List<object>)this.DataSource)
.ConvertAll(item => Convert.ChangeType(item, type))
.OrderBy(item => type.InvokeMember(property.Name, BindingFlags.GetProperty, null, item, null));
任何建议,欢迎:)
编辑:也许我要告诉你的是,第一投(List<object>)this.DataSource
失败,我需要的东西写(List<myType>)this.DataSource
代替。
这可能有助于http://stackoverflow.com/questions/13519910/converting-fieldinfo-value-to-a-list-whe n-list-type-not-known?rq = 1 –
谢谢但不是,对我而言这不是件好事。我需要一种方法来在'List'上做一个orderBy,但是使用反射来初始化这个列表。 –
Shadam
如何定义this.DataSource? –