gpt4 book ai didi

c# - 过滤 Type.GetProperties(),其中 PropertyType.Name 在列表中

转载 作者:行者123 更新时间:2023-12-02 05:05:52 27 4
gpt4 key购买 nike

我只需要显示名称在必填字段列表中的属性。

我正在尝试做这样的事情,但是 p.PropertyType.Name == x 不正确:

Pricing pricing = new Pricing();
Type type = typeof(Pricing);
PropertyInfo[] PricingProperties = type.GetProperties();

PricingRequiredFieldDAL requiredField = new PricingRequiredFieldDAL();

var x = requiredField.GetRequiredFields();

var list = PricingProperties.Where(p => p.PropertyType.Name == x);

public class PricingRequiredFieldDAL
{
PricingContext db = new PricingContext();

public List<PricingRequiredField> GetRequiredFields()
{
return db.PricingRequiredFields.Where(p => p.Required == true).ToList();
}
}

我将如何以上述方式使用反射获取我想要的信息?

最佳答案

就像这样:

var fieldNames = new HashSet<string>(x.Select(p => p.Name));
var properties = PricingProperties.Where(p => fieldNames.Contains(p.Name));

假设 PricingRequiredField 有一个名为 Name 的属性。你实际上并没有告诉我们。

关于c# - 过滤 Type.GetProperties(),其中 PropertyType.Name 在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com