0

I want to modify the behavior a .NET program I did not write. I used ILSpy to have a look at the relevant source code and found the program was using a static field to load the data I wanted to change, so I wrote a simple program to reflect onto the static field, change the value, and then launch the program.

The problem is, the change seems to be ignored by the program when it is launched! I looked around and found that static variables are only static within an AppDomain. I suspect that's my problem. My launcher app is modifying the "static" variable fine, but the call to Process.Start creates a new AppDomain which makes my change irrelevant.

Any way to either:

1) Change a static variable in another AppDomain

or

2) Different approach to solve the problem?

Community
  • 1
  • 1
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
  • What on Earth is Process.Launch()? No amount of reflection or AppDomain hacking is going to do anything when you use Process.Start(). – Hans Passant Jan 31 '13 at 22:35
  • @HansPassant Process.Launch is what happens when you don't proofread your posts. Is there any other way to start the second app which would *allow* me to modify it's values? – just.another.programmer Jan 31 '13 at 22:42

1 Answers1

1

I think you should be able to accomplish this by bootstrapping it.

Create a new application of the same type (Console, WinForms, etc.), add the existing application .EXE as a reference in the new application, and then call into the entry point of the existing application, and it will run just as it did before.

Then, through reflection you should be able to get around any inconvenient access modifiers to be able to modify the values.

I just created a short test app that did exactly this, and it worked fine. I don't know your app, so I'm not sure what alligators are in the weeds, but I think the bootstrapping would give you the best chance here.

J.T. Taylor
  • 4,147
  • 1
  • 23
  • 23