获取列表项的属性

本文关键字:属性 列表 获取 | 更新日期: 2023-09-27 17:57:33

Les说我有一个类:

class SomeObject
{
  public string Name{get;set;}
  public string SomeProperty {get; set;}
}

我还有一个SomeObjects的列表:

List<SomeObject> list1 = new List<SomeObject>();
list1.Add(new SomeObject{ Name="Jhon", SomeProperty="baa bla bla"});
list1.Add(new SomeObject{ Name="Albert", SomeProperty="some description"});
list1.Add(new SomeObject{ Name="Tom", SomeProperty="some text"});

我想创建一个类,通过传递我想要填充的列表视图和列表,我可以在其中填充ListView。因此,该类的构造函数如下:

class MyListView
{
    //Constructor
    public MyListView(System.Windows.Controls.ListView listView, object list)
    {
          Lista = ((IEnumerable)list).Cast<object>().ToList(); // Cast the object list to List<objects>
          var properties = Lista[0].GetType().GetProperties();
          foreach (var prop in properties)
          {
              // prop = Name then SomeProperty in this case
              // create new column in listView
              // etc
          }
    }
}

唯一的问题是,如果传递一个没有对象的列表,我不知道如何获取SomeObject的属性,因为我的构造函数假设列表不是空的,因此通过获取第一个对象,它可以尝试查看属性。

所以我的问题是如何通过查看列表来获得Name和SomeProperty的属性。我想做一些类似的事情:

public MyListView(System.Windows.Controls.ListView listView, object list)
{
   Lista = ((IEnumerable)list).Cast<object>().ToList(); // Cast the object list to List<objects>
   properties = Lista.GetType().GetProperty("some property that refers to list items").GetType().GetProperties();

而不是试图从第一个对象中获取它们。我知道我可以修改构造函数,需要一个构建列表的对象,并从这些对象中获取属性,但如果我不必传递额外的参数,那就太好了。

获取列表项的属性

如果假设list将是"something"的IEnumerable,为什么不继续使用类型参数指定呢。然后,即使列表恰好为null或为空,您也可以获得类型:

public class MyListView<T>{
    //Constructor
    public MyListView(System.Windows.Controls.ListView listView, IEnumerable<T> list)
    {
          var properties = typeof(T).GetProperties();
          foreach (var prop in properties)
          {
              // prop = Name then SomeProperty in this case
              // create new column in listView
              // etc
          }
    }
}
// Example usage: Inferred type parameter 
List<SomeObject> list = null;
var yourListView = new MyListView(listView1, list);
// Example usage: Explicitly specify the type parameter if it can't be inferred
var yourListView = new MyListView<SomeObject>(listView1, null);

当您调用MyListView构造函数时,您知道SomeObject类的类型吗?如果是这样,您可以使用泛型

取自http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx:

        Type d1 = typeof(List<int>);
        Type[] typeParameters = d1.GetGenericArguments();

在这种情况下,typeParametes[0]持有类型System.Int32。

希望这能有所帮助。

您会想要做这样的事情。

if(list.Count>0)
{
    var firstItem = list[0];
    var type = response.GetType();
    PropertyInfo nameInfo = type.GetProperty("Name");
    nameInfo.GetValue(firstItem, null);                 //returns list[0].Name
    nameInfo.SetValue(firstItem, "somevalue", null); ;  //sets list[0].Name to "somevalue"
    PropertyInfo someInfo = type.GetProperty("SomeProperty");
    someInfo.GetValue(firstItem, null);                 //returns list[0].SomeProperty
    someInfo.SetValue(firstItem, "somevalue", null); ;  //sets list[0].SomeProperty to "somevalue"
}

if块确保您没有试图反映空对象。然后我们得到了TypePropertyInfoPropertyInfo允许您访问属性的元数据,还可以获取或设置值。我在setters中使用了"somevalue",但它可以接受任何类型。最后一个参数是,如果您的属性是索引的,但您没有指示它是,那么null将正常工作。

这对于列表中的第一个对象来说效果很好。如果你需要反映整个列表,你需要在一个循环中设置它。