Is there a way to access private and protected members of a class instance?
.NET Reflection can be used to get hold of non-Public members of a class instance. The System.Type class provides various methods that allow you to access the fields/properties/methods for all defined types. Depending on the context, you can use either the typeof(class) operator or the Object.GetType() method to get hold of the System.Type object representing the particular type and then use that type object to access the required member information. The following code shows how to use the Type.GetField() and FieldInfo.GetValue() methods to determine the value of the ‘pasteIndex’ private field implemented by the Diagram.Controller class.
Related Questions
- If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?
- What is the difference between public, protected, and private members of a class?
- Can we create a password protected site with private areas for members only?