I use a Dynamic Assembly to create derived classes at run time. How can I tell, using reflection, whether the base class and individual methods in the base class can be used/called from within the derived class in the dynamic assembly?
+2
A:
There are a number of properties on the MethodInfo
and Type
objects that you can use to query visibility.
For example, for a type you can check IsPublic
, IsPrivate
, IsNotPublic
, IsNested
, IsNestedFamOrAssembly
, and a whole lot more.
For a method (the MethodInfo
object), you've got a similar set: IsPublic
,IsPrivate
, IsFamilyOrAssembly
, etc.
So combine this with information like the Assembly
property on a type (so you can tell if Type1 and Type2 are both in the same assembly) and you should be able to get all of the information you need.
ckramer
2010-06-16 18:09:18
If the .NET framework really provides no direct way to answer my question, is there at least a list of rules somewhere? For example, it would be easy to forget that just because a nested class IsPublic doesn't mean it's accessible, not if the parent class is internal.
Qwertie
2010-06-16 19:36:39
Yeah, thats the problem, there are rules, but you would have to build in code to check them yourself. I'm not even sure of a specific resource that lists what all of the rules are (though I'm sure one exists somewhere). If I were doing this myself I would probably build something that handles the most common cases, and then add in the others as I come across them. Kinda depends on who your audience is as well, though.
ckramer
2010-06-16 20:40:46