Detailed description of access to private members in C #

Source: Internet
Author: User

First, it is not a good practice to access a private member of a class. We all know that Private Members cannot be accessed externally. A class contains many private members, such as private fields, private attributes, and private methods. For private member access, you can use the following method to solve the problem.

  1. Private StringName;
  2. Public StringName
  3. {
  4. Get 
  5. {
  6. ReturnName;
  7. }
  8. Set 
  9. {
  10. Name = value;
  11. }
  12. }

However, sometimes the source code is provided only to your dll. Or you can maintain others' code, but the source code is lost. In this case, you may want to know the value of the Private member, or even directly call the private method in the class. What should we do? It is not very difficult to access private members in. net. This article provides several simple methods for you to do so.

 

To make the code more elegant, use the extension method.

1. Obtain the private field value:

  1. Public StaticT GetPrivateField <T> (This ObjectInstance,StringFieldname)
  2. {
  3. BindingFlags flag = BindingFlags. Instance | BindingFlags. NonPublic;
  4. Type type = instance. GetType ();
  5. FieldInfo field = type. GetField (fieldname, flag );
  6. Return(T) field. GetValue (instance );
  7. }

2. Obtain the private property value:

  1. Public StaticT GetPrivateProperty <T> (This ObjectInstance,StringPropertyname)
  2. {
  3. BindingFlags flag = BindingFlags. Instance | BindingFlags. NonPublic;
  4. Type type = instance. GetType ();
  5. PropertyInfo field = type. GetProperty (propertyname, flag );
  6. Return(T) field. GetValue (instance,Null);
  7. }

3. Set the private member value:

  1. Public Static VoidSetPrivateField (ThisObjectinstance, stringfieldname, objectvalue)
  2. {
  3. BindingFlagsflag = BindingFlags. Instance | BindingFlags. NonPublic;
  4. Typetype = instance. GetType ();
  5. FieldInfofield = type. GetField (fieldname, flag );
  6. Field. SetValue (instance, value );
  7. }

4. Set the private attribute value:

  1. Public Static VoidSetPrivateProperty (ThisObjectinstance, stringpropertyname, objectvalue)
  2. {
  3. BindingFlagsflag = BindingFlags. Instance | BindingFlags. NonPublic;
  4. Typetype = instance. GetType ();
  5. PropertyInfofield = type. GetProperty (propertyname, flag );
  6. Field. SetValue (instance, value,Null

Related Article

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.