Knowing Accessibility of Methods Using Reflection

Knowing Accessibility of Methods Using Reflection

While programming, one inquiry would be to know the if the methods from the given class could be accessible or not from any object, from derived class, from assembly or from derived & assembly…
Knowing ahead for time these factors would reduce the error theater from running.
Say a class with the following definition is given:

class Person{
     private void privateMethod(){}
     public void publicMethod(){}
     protected void protectedMethod(){}
     internal void internalMehtod(){}
     public static void staticMethod(){}
     public virtual void virtualMethod(){}
     protected internal void internalProtectedMethod(){}
}

So, while trying to access the methods of the Person class and wanted to know accessibility – no problem..
Dot Net has provided the following methods for this specific usage under the implementation of reflection

.Name - tells the name of the method
.IsPublic - checks if the method in the class is public or not (public)
.IsStatic - checks if the method is static (static)
.IsVirtual - checks if the method is virtual (virtual)
.IsAssembly - answers if it can be accessed from assembly (internal)
.IsFamily - answers if it can be accessed from derived class (protected)
.IsFamilyOrAssembly - from derived or assebmbly (protected internal)
.IsFamilyAndAssembly - from derived and assembly 

Look @
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.isfamily.aspx For example and application

find longest word in the sentence

Get maximum occurring character

Flatten nested javascript array

Implement Queue Using two Stacks – JavaScript algorithm

Update Node and npm on mac OSX

Tunneling to AWS instance using pem file

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*