Within. NET protected private variables can also be accessed

Source: Internet
Author: User
Tags foreach reflection
Variable | access
Through the original data can be accessed, I was a visit, only to find myself how to operate the private field?

Reference Code

public class L3Data:System.ComponentModel.Component {private System.ComponentModel.Container components = null;  Private Page _page;  Private ArrayList literallist = new ArrayList (); Public L3data (System.ComponentModel.IContainer container) {container.   ADD (this);  InitializeComponent (); }

  public L3data ()   {      initializecomponent ();   }   protected override void Dispose (bool disposing)   {   if (disposing)     {    IF (Components!= null)     {      components. Dispose ();     }   }   base. Dispose (disposing);  }        the code generated #region the Component Designer   private void InitializeComponent ()   {   components = new System.ComponentModel.Container ();   }   #endregion    #region property  public Page page  {    get{return This._page;}    set   {       this._page = value;     type Type = _page. GetType ();     type = type. basetype;  &Nbsp; system.web.ui.webcontrols.literal Literal = new Literal ();     system.reflection.fieldinfo[] fields = type. GetFields (bindingflags.nonpublic| ( bindingflags.static | bindingflags.instance))     foreach (System.Reflection.FieldInfo field in fields)      {     if (field. Fieldtype.equals (literal. GetType ())       this. Literallist.add (field);     }    int Count =0;     foreach (FieldInfo field in this. Literallist)     {     count++;      Object obj = field. GetValue (this. Page);        literal = (literal) obj;     if (literal. Text.equals ("WX"))       literal. Text = "I can you";      else     {    &Nbsp; literal. text= "Wangxing" +count. ToString ();     }    }   }  }    #endregion  


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.