获取属性名称,只需要检索某些列

时间:2012-02-09 20:33:34

标签: c# reflection properties

答案摘要:

使用Jon Skeet的答案解决了这个问题。这是完成的代码

    public static CSVData CreateCSVData(List<RegDataDisplay> rList, 
                                                         string[] selectors)
    {
        CSVData csv = new CSVData(); // Create the CSVData object
        foreach(string selector in selectors)
        {
            // Get the PropertyInfo for the property whose name
            // is the value of selector
            var property = typeof(RegDataDisplay).GetProperty(selector); 

            // Use LINQ to get a list of the values for the specified property
            // of each RegDataDisplay object in the supplied list.
            var values = rList.Select(row => property.GetValue(row, null)
                              .ToString());

            // Create a new list with the property name to use as a header
            List<string> templs = new List<string>(){selector};

            // Add the returned values after the header 
            templs.AddRange(values);

            // Add this list as a column for the CSVData object.
            csv.Columns.Add(templs);
        }
        return csv;
    }

问题

我正在从用户输入动态构建SQL查询,然后将结果导出到CSV文件。我有一个名为RegDataDisplay的类,它为我的查询返回的每个可能列都有一个属性。我可以告诉我们选择哪些列,但在我的CSV创建器中,我只能输出这些特定的列。

在下面的示例中,我检索的所有数据都在rList中,我需要的属性名称在选择器中。所以我想遍历列表,然后只将我需要的属性添加到我的CSV数据中。

public static CSVData CreateCSVData(List<RegDataDisplay> rList, string[] selectors)
{ 
    CSVData csv = new CSVData();
    for(int i = 0; i < selectors.Length; i++)
    {
        csv.Columns.Add(new List<string>(){selectors[i]});
    }

    // So now I have the headers for the CSV columns, 
    // I need the specific properties only which is where I'm stuck

    for(int i = 0; i < selectors.Length; i++)
    {
        for(int j = 0; j < rList.Count; j++)
        {
            // If it was javascript I would do something like this 

            csv.Columns[i].Add(rList[j][selectors[i]]);

        } 
    }
}

由于

编辑:现在正确的轨道,但我遇到了错误“对象与目标类型不匹配”。

    public static CSVData CreateCSVData()
    {
        // I've created a test method with test data

        string[] selectors = new string[] { "Firstname", "Lastname" };
        List<RegDataDisplay> rList = new List<RegDataDisplay>();
        RegDataDisplay rd = new RegDataDisplay();
        rd.Firstname = "first";
        rd.Lastname = "last";
        rList.Add(rd);

        CSVData csv = new CSVData();
        foreach(string selector in selectors)
        {
            var property = typeof(RegDataDisplay).GetProperty(selector);
            var values = rList.Select(row => property.GetValue(rList, null).ToString())
                              .ToList(); // Error throws here
            csv.Columns.Add(values);
        }
        return csv;
    }

1 个答案:

答案 0 :(得分:2)

假设您使用的是.NET 3.5或更高版本,听起来您可能想要这样的内容:

public static CSVData CreateCSVData(List<RegDataDisplay> rList,
                                    string[] selectors)
{ 
    CSVData csv = new CSVData();
    foreach (string selector in selectors)
    {
        var prop = typeof(RegDataDisplay).GetProperty(selector);
        var values = rList.Select(row => (string) prop.GetValue(row, null))
                          .ToList();
        csv.Columns.Add(values);
    }
}