通过反射排除接口实现成员

本文关键字:实现 成员 接口 排除 反射 | 更新日期: 2023-09-27 18:04:47

我有以下接口,并实现:

public interface INew
{
    string TestString { get; }
}
public class PurchaseOrder : INew
{
    public string OrderNo { get; set; }
    public string TestString
    {
        get { return "This is a test string"; }
    }
}

我试图反映出PurchaseOrder对象的OrderNo部分,使用以下代码:

var props = p.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy);
foreach (var prop in props)
{
    Console.WriteLine(prop.Name);
}

我的输出也返回TestString属性。我已经搜索了排除实现接口成员的方法,但只能找到包含它的项。谁能告诉我如何排除这些项目?

通过反射排除接口实现成员

下面是使用GetInterfaceMap方法的解决方案:

var interfaceMethods = typeof(PurchaseOrder)
    .GetInterfaces()
    .Select(x => typeof(PurchaseOrder).GetInterfaceMap(x))
    .SelectMany(x => x.TargetMethods).ToArray();
var propsNotFromInterface= typeof(PurchaseOrder)
    .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy)
    .Where(x => !x.GetAccessors(true).Any(y => interfaceMethods.Contains(y))).ToArray();
Console.WriteLine(propsNotFromInterface.Length);

你可以很容易地把它重构成一个泛型方法。

您可以尝试排除INew中定义的属性,如:

PurchaseOrder p = new PurchaseOrder();
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy;
PropertyInfo[] iNewPropertyInfos = typeof(INew).GetProperties(bindingFlags);
var props = p.GetType().GetProperties(bindingFlags).Where(x => iNewPropertyInfos.All(y => y.ToString() != x.ToString()));
foreach (var prop in props)
{
    Console.WriteLine(prop.Name);
}

更新。

更通用的方法是将逻辑移动到泛型函数中:

private static IEnumerable<PropertyInfo> GetPropertiesExcept<T>(object p)
{
    BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly |
                                BindingFlags.FlattenHierarchy;
    PropertyInfo[] iNewPropertyInfos = typeof (T).GetProperties(bindingFlags);
    return p.GetType().GetProperties(bindingFlags).Where(x => iNewPropertyInfos.All(y => y.ToString() != x.ToString()));
}

和用法:

PurchaseOrder p = new PurchaseOrder();
IEnumerable<PropertyInfo> propertiesExcept = GetPropertiesExcept<INew>(p);