How can I determine the accessibility of a MemberInfo instance?

Viewed 1061

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)?

2 Answers

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

Related