Search code examples
c#.netappdomain

Accessing Internal field of nested class from different AppDomain


This question is fairly simple in scope, it boils down to the following setup:

public class B : MarshalByRefObject {
    public A a;
    public class A : MarshalByRefObject {
        internal int c;
        public int d;
    }
}

where I have an instance of B in one AppDomain with a reference to A in another appdomain. However, I'm unable to reference c from B by calling B.a.c; if I do, I get a remotingexception "Remoting cannot find field c on type A". I can access d by calling B.a.d, though. Is there any way to get c without resorting to reflection or is this simply a result of using AppDomains?

Funny enough, when I run the code in Mono 4.2.3 I get no exceptions. It's only on windows that I have issues, likely due to CAS.


Solution

  • From MSDN:

    Internal types or members are accessible only within files in the same assembly

    Since remoting involves creating a reference from your client to the assembly containing the remotable objects, internal members will not be visible to your client class library. You aren't going to get around accessing internal members without resorting to reflection or a combination of dynamics and reflection.