Private methods are closed? Use reflection to invoke a private method of an object.

Source: Internet
Author: User

When we learn C # The first set of keywords to contact may be Private, public, protect.

Private is defined as: methods and variables defined with this keyword can only be used inside an object.

But is this absolute? Is there a way to use private defined methods or variables in the caller's space?

Let's define one of the following classes:

  1. Public class Testobj
  2. {
  3. Public string Publicvalue { get; set; }
  4. Private string _privatevalue;
  5. public Testobj ()
  6. {
  7. _privatevalue = "private";
  8. Publicvalue = "public";
  9. }
  10. Public testobj (string value)
  11. {
  12. _privatevalue = "private_" + value;
  13. Publicvalue = "public_" + value;
  14. }
  15. Private string returnprivatevalue ()
  16. {
  17. return _privatevalue;
  18. }
  19. }

So can we access _priavatevalue in this simple program?

    1. Static void Main (string[] args)
    2. {
    3. Testobj to = new testobj ("test");
    4. Console.WriteLine ("obj public parameter:{0}" to. Publicvalue);
    5. Console.WriteLine ("obj public parameter:{0}", to. Returnprivatevalue ());
    6. Console.read ();
    7. }

We get a compile error when we try to compile this simple program.

' PrivateCallByReflection.testObj.returnPrivateValue () ' is inaccessible due to its protection level

So private is really safe, can only be accessed internally?

A little trick can get the results we want.

  1. Static void Main (string[] args)
  2. {
  3. Testobj to = new testobj ("test");
  4. Console.WriteLine ("obj public parameter:{0}" to. Publicvalue);
  5. //console.writeline ("obj Public parameter:{0}", To.returnprivatevalue ());
  6. MethodInfo Privatemethod = typeof(testobj). GetMethod ("returnprivatevalue", BindingFlags.Instance | BindingFlags.NonPublic);
  7. Console.WriteLine ("obj Private method ' Returnprivatevalue ' return: {0}", Privatemethod.invoke (to, new Object[] {});
  8. Console.read ();
  9. }

Please note the underlined code.

Reflection helps us to access a private method.

No lower limit of reflection, really there is nothing to do .....

Private methods are closed? Use reflection to invoke a private method of an object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.