Reflection--how can I determine the visibility of a type or member?

  • Thread starter Thread starter Jeff Johnson [MVP: VB]
  • Start date Start date
J

Jeff Johnson [MVP: VB]

What do I need to do to figure out if a class, enum, etc. is public,
private, protected, Friend, etc.?
 
Jeff Johnson said:
What do I need to do to figure out if a class, enum, etc. is public,
private, protected, Friend, etc.?

Use Type.GetAttributeFlagsImpl - look at the TypeAttributes enumeration
for more detail.
 
Wow. That was not intuitive. Thanks.

Well it's not the only way, you can also use the Is<AccessLevel>
(IsPublic, IsNotPublic, IsNestedFamily and so on) on the Type.



Mattias
 
Mattias Sjögren said:
Well it's not the only way, you can also use the Is<AccessLevel>
(IsPublic, IsNotPublic, IsNestedFamily and so on) on the Type.

Unfortunately that doesn't distinguish (for non-nested types) between
internal, protected and private, as far as I can see.

(However, the docs for IsNotPublic suggest looking at VisibilityMask,
which leads to the TypeAttributes enumeration - from there, finding
GetAttributesImpl isn't too hard.)
 
Jon,
Unfortunately that doesn't distinguish (for non-nested types) between
internal, protected and private, as far as I can see.

There are only two levels that can be used for non-nested types,
public (IsPublic) or not (IsNotPublic). The non-public level is called
private in ILAsm, internal in C# and Friend in VB.NET. Protected
doesn't make sense if the type isn't nested.



Mattias
 
Mattias Sjögren said:
There are only two levels that can be used for non-nested types,
public (IsPublic) or not (IsNotPublic). The non-public level is called
private in ILAsm, internal in C# and Friend in VB.NET. Protected
doesn't make sense if the type isn't nested.

True. Not sure why that hadn't occurred to me before. Doh!
 
Back
Top