英文:
Get class properties of a specific type
问题
我有一个C#的WinForm项目。我有一个名为TopProducts的类,它有许多DataTable属性,并且我创建了一个这个类的集合。
我想要遍历这个集合并创建一个List<DataTable>。你可以在下面的GetListOfDataTables方法的注释中找到详细信息。
public class TopProducts
{
    public Guid ID { get; set; }
    public string No { get; set; }
    public string ProductName { get; set; }
    public int ProductQty { get; set; }
    public int Level { get; set; }
    public DataTable dtProfile { get; set; }
    public DataTable dtPBMAndGlass { get; set; }
    public DataTable dtArticals { get; set; }
    public DataTable dtCutting { get; set; }
    public DataTable dtCNC { get; set; }
    public DataTable dtIFS { get; set; }
    public DataTable dtIFSGlassList { get; set; }   
    // public DataTable MoreDataTable
}
public List<DataTable> GetListOfDataTables(List<TopProducts> ProductList)
{
    List<DataTable> dtList = new List<DataTable>();
    foreach (TopProducts product in ProductList)
    {
        //我需要类似下面的代码
        //if (product.GetType().Equals(DataTable))
        //{
            // dtList.Add(product) 
        //}
    }
    return dtList;
}
英文:
I have a WinForm project in c#. I have class named TopProducts that has many DataTable properties and I create a collection of this class.
I want to loop over this collection and create List<Datatable>. You can find details in the comments in the GetListOfDataTables method below.
public class TopProducts
{
    public Guid ID { get; set; }
    public string No { get; set; }
    public string ProductName { get; set; }
    public int ProductQty { get; set; }
    public int Level { get; set; }
    public DataTable dtProfile { get; set; }
    public DataTable dtPBMAndGlass { get; set; }
    public DataTable dtArticals { get; set; }
    public DataTable dtCutting { get; set; }
    public DataTable dtCNC { get; set; }
    public DataTable dtIFS { get; set; }
    public DataTable dtIFSGlassList { get; set; }   
    // public DataTable MoreDataTable
}
public List<DataTable> GetListOfDataTables(List<TopProducts> ProductList)
{
    List<DataTable> dtList = new List<DataTable>();
    foreach (TopProducts product in ProductList)
    {
        //I need codes something similar below
        //if (product.GetType().Equals(DataTable))
        //{
            // dtList.Add(product) 
        //}
    }
    return dtList;
}
答案1
得分: 1
似乎你的意思是使用反射来从给定的List<TopProducts>中提取DataTable属性的值并将它们展平成一个序列。如果是这样,你可以这样做:
// +
using System.Reflection;
public IEnumerable<DataTable> GetListOfDataTables(IEnumerable<TopProducts> ProductList) 
    => ProductList
    .SelectMany(topProduct => topProduct.GetType()
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(propInfo => propInfo.PropertyType == typeof(DataTable))
    .Select(propInfo => propInfo.GetValue(topProduct) as DataTable))
    .Where(dt => dt != null);
英文:
It seems you mean using Reflection to extract the values of the DataTable properties from a given List<TopProducts> and flatten them in one sequence. If so, you could have:
// +
using System.Reflection;
public IEnumerable<DataTable> GetListOfDataTables(IEnumerable<TopProducts> ProductList) 
    => ProductList
    .SelectMany(topProduct => topProduct.GetType()
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(propInfo => propInfo.PropertyType == typeof(DataTable))
    .Select(propInfo => propInfo.GetValue(topProduct) as DataTable))
    .Where(dt => dt != null);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论