views:

30

answers:

1

Hi,

Is it possible to change the value of a public property (type string) of a class within a given .NET AppDomain from another separate .NET AppDomain assuming both AppDomain's are running in the same process. The other important assumption is that the code running in the AppDomain that contains the property can not be modified .. ie .. recompiled with any changes.

Thanks!

+1  A: 

Sure. You just need to expose access to the property via a MarshalByRef object that will allow one instance in AppDomain A to reach out and touch the property in AppDomain B.

Here is a simple example of the class that would be instantiated in AppDomain B from AppDOmain A:

   internal class SomeLinkClass : MarshalByRefObject
   {
      internal void UpdateProperty(string newValue)
      {
         // this function actually will execute within AppDomain B

         // somehow get access to the property and then set it 
         // with the new value.
      }
   }

And here is how you would consume it from AppDomain A:

// somehow you need to get a ref to AppDomain B
    SomeLinkClass linkClass = appDomainB.CreateInstanceFromAndUnwrap(
                                  Assembly.GetExecutingAssembly().Location,
                                  typeof(SomeLinkClass).FullName) as SomeLinkClass;
Russell McClure
@Russell - I should clarify that you have to assume you can not architecturally modify the AppDomain that contains the property you are attempting to change.
Doug
@Doug: I've updated my answer since your comment so let me know if the solution still doesn't work and why. Then maybe we can refine it.
Russell McClure
@Russell - I am not able to make any changes to the code - If I could I agree your answer would likely be helpful - I am thinking this might need to be done though reflection - Thanks though
Doug
@Doug: My way doesn't require any changes to the code in AppDomain B. Two questions, 1: Is the property you are attempting to modify a static or an instance property? 2: If it's an instance property then how will you find the appropriate instance which has the property you want to modify?
Russell McClure
@Russell - So basically AppDomain A only has to stay unchanged, AppDomian B represents a small app to change the instance memeber in A. There is only a single object instance of the class that contains the property in AppDomain A.
Doug
@Doug: You are confusing A and B. B is the one you can't alter. B contains the instance with the property you want to change. Can you clearly state how you are going to get to the proper instance in B? The only way I can see it working is either the property itself is static or there is a static property that returns the instance you want to modify.
Russell McClure