查看MemberInfo是否使用C#匹配BindingFlags

时间:2019-05-22 05:46:14

标签: c# reflection bindingflags

我需要查看MemberInfo是否匹配特定的BindingFlags。与此最接近的方法是Type#GetMember(string, BindingFlags)

我找不到任何方法可以做到这一点。

我想做这样的事情:

private List<MemberInfo> _members;

public IEnumerable<MemberInfo> GetMembers(BindingFlags flags)
{
    foreach(var member in _members)
    {
        if(member.MatchesFlags(flags))
        {
             yield return member;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

通过反射(例如,通过在MemberInfo上调用MemberInfo[] GetMembers())获得的Type的实际类型是:

  • System.Reflection.RuntimeMethodInfo
  • System.Reflection.RuntimeConstructorInfo
  • System.Reflection.RuntimePropertyInfo
  • System.Reflection.RtFieldInfo

所有它们独立地具有类型BindingFlags的属性BindingFlags。但是该属性为非公共。并且类型为internal sealed,这使得它们在常规代码中不可访问reflection在这里进行救援。要获得BindingFlags中的MemberInfo,可以使用这样的扩展名:

public static class MemberInfoExtension
{
    private static readonly Dictionary<MemberInfo, BindingFlags> cache =
        new Dictionary<MemberInfo, BindingFlags>();
    private static readonly BindingFlags flags =
        BindingFlags.Instance | BindingFlags.NonPublic;

    public static BindingFlags GetFlags(this MemberInfo memberInfo)
    {
        if (cache.TryGetValue(memberInfo, out var bindingFlags))
            return bindingFlags;

        return cache[memberInfo] =
            (BindingFlags)memberInfo.GetType()
                                    .GetProperty("BindingFlags", flags)
                                    .GetValue(memberInfo);
    }
}

要使用BindingFlags查找匹配项,可以使用以下辅助方法:

public static class BindingFlagsExtension
{
    public static bool Contains(this BindingFlags flags, BindingFlags bindingFlags) =>
        (flags & bindingFlags) == bindingFlags;

    public static bool MatchesExactly(this BindingFlags flags, BindingFlags bindingFlags) =>
        flags == bindingFlags;

    public static bool MatchesPartly(this BindingFlags flags, BindingFlags bindingFlags) =>
        (flags & bindingFlags) != 0;
}

要测试解决方案,可以使用这样的类:

private class DemoClass
{
    private int PrivateInstanceField;
    public string PublicInstanceField;
    private bool PrivateInstanceProperty { get; }
    public object PublicInstanceProperty { get; }
    private void PrivateInstanceMethod() { }
    public void PublicInstanceMethod() { }
    private static int PrivateStaticField;
    public static string PublicStaticField;
    private static bool PrivateStaticProperty { get; }
    public static object PublicStaticProperty { get; }
    private static void PrivateStaticMethod() { }
    public static void PublicStaticMethod() { }
}

然后运行:

static void Main(string[] args)
{
    var type = typeof(DemoClass);
    var members = type.GetMembers();
    var flags = BindingFlags.Public | BindingFlags.Instance;

    Console.WriteLine($"{type.Name} members with flags containing: {flags}\n");
    foreach (var m in members.Where(m => m.GetFlags().Contains(flags)))
        Print(m);

    Console.WriteLine($"\n{type.Name} members with flags matching exactly: {flags}\n");
    foreach (var m in members.Where(m => m.GetFlags().MatchesExactly(flags)))
        Print(m);

    Console.WriteLine($"\n{type.Name} members with flags matching partly: {flags}\n");
    foreach (var m in members.Where(m => m.GetFlags().MatchesPartly(flags)))
        Print(m);
}

private static void Print(MemberInfo memberInfo) =>
    Console.WriteLine($"\t{memberInfo.GetType().Name} {memberInfo} - {memberInfo.GetFlags()}");

给予:

DemoClass members with flags containing: Instance, Public

        RuntimeMethodInfo System.Object get_PublicInstanceProperty() - Instance, Public
        RuntimeMethodInfo Void PublicInstanceMethod() - Instance, Public
        RuntimeMethodInfo Boolean Equals(System.Object) - DeclaredOnly, Instance, Public
        RuntimeMethodInfo Int32 GetHashCode() - DeclaredOnly, Instance, Public
        RuntimeMethodInfo System.Type GetType() - DeclaredOnly, Instance, Public
        RuntimeMethodInfo System.String ToString() - DeclaredOnly, Instance, Public
        RuntimeConstructorInfo Void .ctor() - Instance, Public
        RuntimePropertyInfo System.Object PublicInstanceProperty - Instance, Public
        RtFieldInfo System.String PublicInstanceField - Instance, Public

DemoClass members with flags matching exactly: Instance, Public

        RuntimeMethodInfo System.Object get_PublicInstanceProperty() - Instance, Public
        RuntimeMethodInfo Void PublicInstanceMethod() - Instance, Public
        RuntimeConstructorInfo Void .ctor() - Instance, Public
        RuntimePropertyInfo System.Object PublicInstanceProperty - Instance, Public
        RtFieldInfo System.String PublicInstanceField - Instance, Public

DemoClass members with flags matching partly: Instance, Public

        RuntimeMethodInfo System.Object get_PublicInstanceProperty() - Instance, Public
        RuntimeMethodInfo Void PublicInstanceMethod() - Instance, Public
        RuntimeMethodInfo System.Object get_PublicStaticProperty() - Static, Public
        RuntimeMethodInfo Void PublicStaticMethod() - Static, Public
        RuntimeMethodInfo Boolean Equals(System.Object) - DeclaredOnly, Instance, Public
        RuntimeMethodInfo Int32 GetHashCode() - DeclaredOnly, Instance, Public
        RuntimeMethodInfo System.Type GetType() - DeclaredOnly, Instance, Public
        RuntimeMethodInfo System.String ToString() - DeclaredOnly, Instance, Public
        RuntimeConstructorInfo Void .ctor() - Instance, Public
        RuntimePropertyInfo System.Object PublicInstanceProperty - Instance, Public
        RuntimePropertyInfo System.Object PublicStaticProperty - Static, Public
        RtFieldInfo System.String PublicInstanceField - Instance, Public
        RtFieldInfo System.String PublicStaticField - Static, Public