【发布时间】:2014-10-19 08:16:24
【问题描述】:
我正在将数据集转换为动态集合并绑定它,这工作正常。现在当我需要向集合添加一个空的新对象时。我正在尝试获取数据网格的 ItemsSource 并获取列表中的第一个对象。但它里面有一些价值。如何删除值并使用反射绑定一个空对象。
这是我的代码,
IEnumerable<object> collection = this.RetrieveGrid.ItemsSource.Cast<object>();
List<object> list = collection.ToList();
//i need to clear the values inside list[0]
object name = list[0];
//here i build the properties of the object, now i need to create an empty object using these properties and add it to the list
PropertyInfo[] pis = list[0].GetType().GetProperties();
【问题讨论】:
-
我不确定我是否理解您的问题。如果你使用反射来获取未知类型的属性,那么你可以使用反射来获取构造函数并实例化它。
-
我只想使用这些属性创建一个空对象
-
您在寻找
Activator.CreateInstance(list[0].GetType())吗? -
@SriramSakthivel 谢谢,我怎样才能为它里面的属性设置空值?
-
空值是什么意思?你的意思是默认值?默认情况下,如果您的类没有在构造函数或字段初始化程序中设置任何内容,那么它只会是默认值,不是吗?
标签: c# wpf reflection silverlight-4.0 dataset