C# 如何获取完成在属性值中建立的requisit的所有类属性?

C# 如何获取完成在属性值中建立的requisit的所有类属性?,c#,reflection,properties,filter,attributes,C#,Reflection,Properties,Filter,Attributes,为了清楚起见,举个小例子: public class Book { [MyAttrib(isListed = true)] public string Name; [MyAttrib(isListed = false)] public DateTime ReleaseDate; [MyAttrib(isListed = true)] public int PagesNumber; [MyAttrib(isListed = false)

为了清楚起见,举个小例子:

public class Book
{
    [MyAttrib(isListed = true)]
    public string Name;

    [MyAttrib(isListed = false)]
    public DateTime ReleaseDate;

    [MyAttrib(isListed = true)]
    public int PagesNumber;

    [MyAttrib(isListed = false)]
    public float Price;
}
问题是:如果在
MyAttrib
上设置了bool参数
isListed
,如何仅获取属性

这就是我得到的:

PropertyInfo[] myProps = myBookInstance.
                         GetType().
                         GetProperties().
                         Where(x => (x.GetCustomAttributes(typeof(MyAttrib), false).Length > 0)).ToArray();
myProps
获取了
Book
中的所有属性,但现在,当其
isListed
参数返回false时,我无法确定如何排除它们

foreach (PropertyInfo prop in myProps)
{
    object[] attribs = myProps.GetCustomAttributes(false);

    foreach (object att in attribs)
    {
        MyAttrib myAtt = att as MyAttrib;

        if (myAtt != null)
        {
            if (myAtt.isListed.Equals(false))
            {
                // if is true, should I add this property to another PropertyInfo[]?
                // is there any way to filter?
            }
        }
    }
}

任何建议都将非常宝贵。提前感谢。

我认为使用Linq的查询语法会更容易一些

var propList = 
    from prop in myBookInstance.GetType()
                               .GetProperties()
    let attrib = prop.GetCustomAttributes(typeof(MyAttrib), false)
                     .Cast<MyAttrib>()
                     .FirstOrDefault()
    where attrib != null && attrib.isListed
    select prop;
var-propList=
从myBookInstance.GetType()中的prop
.GetProperties()
让attrib=prop.GetCustomAttributes(typeof(MyAttrib),false)
.Cast()
.FirstOrDefault()
attrib在哪里!=null&&attrib.isListed
选择道具;

我认为使用Linq的查询语法会容易一些

var propList = 
    from prop in myBookInstance.GetType()
                               .GetProperties()
    let attrib = prop.GetCustomAttributes(typeof(MyAttrib), false)
                     .Cast<MyAttrib>()
                     .FirstOrDefault()
    where attrib != null && attrib.isListed
    select prop;
var-propList=
从myBookInstance.GetType()中的prop
.GetProperties()
让attrib=prop.GetCustomAttributes(typeof(MyAttrib),false)
.Cast()
.FirstOrDefault()
attrib在哪里!=null&&attrib.isListed
选择道具;