简体   繁体   English

获取列表项的属性

[英]Get properties of List item

Les's say I have a class:比如说我有一个 class:

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

I also have a list of SomeObjects's:我还有一个 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"});

I want to create a class where I can populate a ListView by passing the listview that I want to fill and also a list.我想创建一个 class ,我可以通过传递我要填充的列表视图和列表来填充列表视图。 therefore my constructor of that class is like:因此我的 class 的构造函数就像:

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
          }

    }
}

The only problem is that if a pass a list with no objects I don't know how could I get the properties of SomeObject because my constructor is assuming that the list is not empty therefore by getting the first object it can try to see the properties.唯一的问题是,如果传递一个没有对象的列表,我不知道如何获得 SomeObject 的属性,因为我的构造函数假设列表不为空,因此通过获取第一个 object 它可以尝试查看属性.

So my question is how could I get the properties of Name and SomeProperty by looking at the list.所以我的问题是如何通过查看列表来获取 Name 和 SomeProperty 的属性。 I am looking to do something like:我想做类似的事情:

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();

Instead of trying to get them from the first object.而不是试图从第一个 object 中获取它们。 I know I could modify the constructor and require an object of which the list is constructed and get the properties from that objects but it will be nice if I don't have to pass that extra parameter.我知道我可以修改构造函数并需要一个 object,其中构造了列表并从该对象中获取属性,但如果我不必传递那个额外的参数会很好。

If you're making the assumption that the list is going to be an IEnumerable of "something", why not go ahead and specify that by using a type parameter.如果您假设list将是“某物”的IEnumerable ,为什么不提前 go 并使用类型参数指定它。 Then you can get the type even if the list happens to be null or empty:然后即使列表恰好是 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);

When you call the MyListView constructor, do you know the type of the SomeObject class?调用 MyListView 构造函数时,你知道 SomeObject class 的类型吗? If so, you could use generics如果是这样,您可以使用 generics

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

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

In this case, typeParametes[0] holds the type System.Int32.在这种情况下,typeParametes[0] 拥有 System.Int32 类型。

Hope this helps.希望这可以帮助。

You'll want to do something like this.你会想做这样的事情。

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"
}

The if block makes sure that you arent trying to reflect on a null object. if块确保您不会试图反映 null object。 Then we get the Type and the PropertyInfo from there.然后我们从那里得到TypePropertyInfo The PropertyInfo allows you to access the meta data for the property and also get or set the value. PropertyInfo允许您访问属性的元数据并获取或设置值。 I used "somevalue" in the setters but it can accept any type.我在设置器中使用了"somevalue" ,但它可以接受任何类型。 The last parameter is if your property is indexed, but you didnt indicate that it was so null will work fine.最后一个参数是你的属性是否被索引,但你没有指出它是这样null可以正常工作。

This works just fine for the first object in the list.这适用于列表中的第一个 object。 If you need to reflect over the entire list, you will need to set this up in a loop.如果您需要反映整个列表,则需要在循环中进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM