从PropertyInfo []中删除属性

San*_*San 1 c# reflection

从"属性"中删除前四个属性的最简单方法是什么?其中属性是PropertyInfo集合,如下所示.

PropertyInfo[] properties = GetAllPropertyForClass(className);

public static PropertyInfo[] GetAllPropertyForClass(string className) {
    Type[] _Type = Assembly.GetAssembly(typeof(MyAdapter)).GetTypes();

    return _Type.SingleOrDefault(
                t => t.Name == className).GetProperties(BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly);   
}
Run Code Online (Sandbox Code Playgroud)

当然,我可以通过根据索引或名称忽略属性来循环并构建另一个PropertyInfo []集合.但我想知道是否有任何方法可以实现没有循环通过属性.

谢谢

Hei*_*nzi 7

LINQ帮助:

PropertyInfo[] almostAllProperties = properties.Skip(4).ToArray();
Run Code Online (Sandbox Code Playgroud)

这适用于各种IEnumerables,而不仅仅是PropertyInfo的数组.


编辑:正如其他人所指出的那样,按名称排除属性更加强大.以下是使用LINQ的方法:

PropertyInfo[] almostAllProperties = properties.Where(
    p => p.Name != "ExcludeProperty1"
        && p.Name != "ExcludeProperty2"
        && p.Name != "ExcludeProperty3").ToArray();
Run Code Online (Sandbox Code Playgroud)