我需要查看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;
}
}
}
通过反射(例如通过Type
调用MemberInfo[]
GetMembers()
)获得的实际MemberInfo
类型为:
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