Search code examples
windows-phone-7reflectionprivate-members

WP7 - access private variables


Imagine this:

Class A{
private int a;
}

Class B{
  public void fn(){
    A obj=new A();
    // access obj's private variable a here.
  }
}

I want to access obj's private variable a, as commented out. Reflection doesn't work as it throws a fieldaccessexception. I want to access a. Is there any other way to do it? I cannot change access modifier.


Solution

  • You can only access public, internal or protected members (that is, fields, methods, etc.) on Windows Phone.

    Reflection on Windows Phone does not allow access to private members. I would recommend you use "internal" for internal members you wish to access.