简体   繁体   English

筛选器Type.GetProperties(),其中PropertyType.Name在列表中

[英]Filter Type.GetProperties() where PropertyType.Name is in a List

I need to show only the properties that have names that are in the requiredfield list. 我只需要显示名称在requiredfield列表中的属性。

I'm trying to do something like this but p.PropertyType.Name == x is not correct: 我正在尝试做这样的事情,但是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();
    }
}

How would I go about getting the information I want using reflection in the above fashion? 我将如何以上述方式使用反射来获取想要的信息?

Just something like: 就像这样:

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

That's assuming that PricingRequiredField has a property called Name . 假设PricingRequiredField具有一个名为Name的属性。 You haven't actually told us that. 您实际上没有告诉过我们。

Try this: 尝试这个:

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

I supposed x is a list. 我以为x是一个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM