【问题标题】:Get properties of List item获取列表项的属性
【发布时间】:2011-08-31 10:43:53
【问题描述】:

假设我有一堂课:

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

我想创建一个类,我可以通过传递我要填充的列表视图和列表来填充列表视图。因此我的那个类的构造函数是这样的:

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

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

【问题讨论】:

    标签: c# reflection properties


    【解决方案1】:

    如果您假设list 将是“某物”的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);
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

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

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:

          你会想做这样的事情。

          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 允许您访问属性的元数据并获取或设置值。我在设置器中使用了"somevalue",但它可以接受任何类型。最后一个参数是你的属性是否被索引,但你没有表明它是这样null 可以正常工作。

          这适用于列表中的第一个对象。如果您需要反映整个列表,则需要在循环中进行设置。

          【讨论】:

          • 如果我能在不参考 [n] 项的情况下获得这些属性,那就太好了。如果列表为空,我仍然希望能够仅使用列而不是行来填充列表视图
          • 我不知道列表将提供什么类型的对象,这就是我使用反射的原因。
          猜你喜欢
          • 2011-04-28
          • 1970-01-01
          • 2012-02-23
          • 2015-05-27
          • 2019-05-29
          • 2021-03-26
          • 2013-11-06
          • 2020-01-11
          • 1970-01-01
          相关资源
          最近更新 更多