【问题标题】:How to get List of Properities from System.Collections.IEnumerable using LINQ C#如何使用 LINQ C# 从 System.Collections.IEnumerable 获取属性列表
【发布时间】:2016-03-29 11:12:03
【问题描述】:

我需要使用 WPF 中 DataGrid 控件的 System.Collections.IEnumerable 获取模型类属性列表。

DataGrid 控件有一个属性public IEnumerable ItemsSource { get; set; }

考虑为 ItemsSource 分配了以下列表:List<Person> EmpList

void Main()
{
    List<Person> EmpList = new List<Person>()
    {
        new Person() {ID = 101, Name = "Peter", Gender = "Male", Role = "Manager"},
        new Person() {ID = 102, Name = "Emma Watson", Gender = "Female", Role = "Assistant"},
        new Person() {ID = 103, Name = "Kaliya", Gender = "Manager", Role = "Assistant"},
    };
}

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string Role { get; set; }
}

我需要使用 public IEnumerable ItemsSource { get; set; } 获取属性名称

我用List&lt;Person&gt; EmpList试了一下

EmpList.GetType()
    .GetInterfaces()
    .Where(t => t.IsGenericType == true
        && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
    .Select(t => t.GetGenericArguments()[0])
    .FirstOrDefault().GetProperties()
    .Select(x => x.Name).ToList();

上述查询的输出是

像这样我需要使用 DataGrid ItemsSource

获取模型属性

我在 Datagrid 控件类中使用this.ItemsSource 进行了尝试,但无法获取列表。但是 ItemsSource 填充了 3 个集合,与上述集合相同。

this.ItemsSource.GetType()
    .GetInterfaces()
    .Where(t => t.IsGenericType == true
        && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
    .Select(t => t.GetGenericArguments()[0])
    .FirstOrDefault().GetProperties()
    .Select(x => x.Name).ToList().Dump();

我需要与上面提到的相同的输出,使用this.ItemsSource。请帮助我如何实现这一目标???

我尝试了@bhuvin 方法 - Reflection,但结果为空

屏幕截图 1:ItemsSource 已正确加载 List

屏幕截图 2:我得到了空的结果集

类型信息和属性信息逐步快照

类型信息:

物业信息:

【问题讨论】:

    标签: c# wpf linq datagrid itemssource


    【解决方案1】:
        using System.Reflection;  // reflection namespace
    
        // get all public static properties of ObjList's type
        PropertyInfo[] propertyInfos;
        //objList might be any type of Collection.
        propertyInfos = objList.GetType().GetProperties(BindingFlags.Public |
                                                      BindingFlags.Static);
        // sort properties by name
        Array.Sort(propertyInfos,
                delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
                { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
    
        // write property names
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
          Console.WriteLine(propertyInfo.Name);
        }
    

    【讨论】:

    • 我不知道类名。如何实现这一点。因为在 DataGrid 中我们只是传递集合。那我怎么能做到这一点?请给我解释一下...
    • @IRPunch :试试这个。这是一个通用的想法。你必须实现它与你相关。
    • 我在我的帖子中添加了屏幕截图。请参考。我有一个空集...请帮助我
    • @IRPunch :您能否附上以下屏幕截图: 1. GetType() 调用返回的类型。 2. GetProperties() 结果是什么?
    • 请参考帖子,我根据您的查询使用屏幕截图进行了编辑。
    【解决方案2】:

    假设 PersonsGrid 是 Datagrid 控件的名称,则以下代码应返回所需的数据:

    Type itemType = PersonsGrid.ItemsSource.GetType().GetGenericArguments()[0];
    
    var propertyNames = itemType.GetProperties().Select(x => x.Name);
    

    【讨论】:

    • 它返回空字符串集。
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多