C# – Reflecting private Remoted Objects

creflectionremoting

I want to return a private field from a remoted object but i get the exception:

RemotingException was caught
Remoting cannot find field 'connectionString' on type 'DBGeneral'.

I DO get the Private field's FieldInfo object when executing the GetField() method.

FieldInfo field = o.GetType().GetField("connectionString", BindingFlags.Instance | BindingFlags.NonPublic);

But its when execute GetValue(), that the RemotingException is thrown.

field.GetValue(o);

If i turn remoting off and reflect the local private connectionString field, i get the string returned to me.

Best Answer

This makes some assumptions about how you are remoting, may not be right but would explain your error.

When your object is sent over remoting it must be serialized. The serializer can only "see" the public properties, therefore private properties do not get sent over the wire.

Edit: Based on Comment

You have atleast 2 options:

The simple one is to make the property public.

One that is much more work is to switch from using remoting to using WCF. Then you can mark your private variable like this:

[DataMember(Name="SomeValue")]
private int m_SomeValue;
Related Topic