获取特定类型的类属性

huangapple go评论81阅读模式
英文:

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&lt;Datatable&gt;. 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&lt;DataTable&gt; GetListOfDataTables(List&lt;TopProducts&gt; ProductList)
{
    List&lt;DataTable&gt; dtList = new List&lt;DataTable&gt;();
    foreach (TopProducts product in ProductList)
    {
        //I need codes something similar below
        //if (product.GetType().Equals(DataTable))
        //{
            // dtList.Add(product) 
        //}
    }
    return dtList;
}

答案1

得分: 1

似乎你的意思是使用反射来从给定的List&lt;TopProducts&gt;中提取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&lt;TopProducts&gt; and flatten them in one sequence. If so, you could have:

// +
using System.Reflection;

public IEnumerable&lt;DataTable&gt; GetListOfDataTables(IEnumerable&lt;TopProducts&gt; ProductList) 
    =&gt; ProductList
    .SelectMany(topProduct =&gt; topProduct.GetType()
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(propInfo =&gt; propInfo.PropertyType == typeof(DataTable))
    .Select(propInfo =&gt; propInfo.GetValue(topProduct) as DataTable))
    .Where(dt =&gt; dt != null);

huangapple
  • 本文由 发表于 2023年6月18日 23:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定