I know the BindingFlags are used to fetch public and non-public members from a Type.
But is there a way to determine if a MemberInfo instance (or derived like PropertyInfo, MethodInfo) is public or not (after it was returned from one of the methods on Type)?
PropertyInfo
, MethodBase
etc each have an Attributes
property which has this information - but there's nothing in MemberInfo
, because each kind of member has its own kind of attributes enum. Hideous as it is, I think you may need to treat each subclass of MemberInfo
separately :( You can probably switch on MemberInfo.MemberType
and then cast, which will be slightly nicer than lots of as
/test-for-null branches, but it's still not ideal :(
if (member.MemberType == MemberTypes.Property)
{
var property = (PropertyInfo) member;
...
}
You can try ie:
var isPublic = memberInfo.MemberType switch
{
MemberTypes.Field => ((FieldInfo)memberInfo).IsPublic,
MemberTypes.Property => ((PropertyInfo)memberInfo).GetAccessors().Any(MethodInfo => MethodInfo.IsPublic),
_ => false
};
For properties this return true if there is any public accessor which I think is what you're after
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With