Is the private method closed? Use reflection to call the private method of an object ., Private closed
When we learn C #, the first set of keywords that we encounter may be Private, Public, and protect.
The definition of Private is: the methods and variables defined with this keyword can only be used inside the object.
But is this absolute? Is there a way to use Private-defined methods or variables in the caller's space?
Let's define the following class:
Can we access _ priavateValue in this simple program?
When we try to compile this simple program, we will get a compilation error.
'Privatecallbyreflection. testObj. returnPrivateValue () 'is inaccessible due to its protection level
So private is actually safe and can only be accessed internally?
A small trick can get the desired result.
Pay attention to the underlined code.
Reflection helps us access a Private method.
There is no lower limit of reflection. There is really nothing to do with it .....