Somehow I assumed that delegates passed to another AppDomain would turn into a proxy as if it were an object derived from MarshalByRefObject
. Unfortunately, it seems they don’t.
Let’s say in my code I have a class MyClass
like this:
[Serializable]
public sealed class MyClass
{
public Func<Input, Output> SomeDelegate;
}
[Serializable]
public sealed class Input { ... }
[Serializable]
public sealed class Output { ... }
Now I need to pass an instance of MyClass
to another AppDomain.
The problem is that the delegate stored in SomeDelegate
may contain a reference to pretty much any method, including potentially a method on an instance of a type that is neither [Serializable]
nor derived from MarshalByRefObject
.
For the sake of this question, let’s assume that I cannot change the code that creates the delegate, nor can I make MyClass
a MarshalByRefObject
. It is, however, [Serializable]
.
(Note that if MyClass
contained a field of a type that derives from MarshalByRefObject
, the object stored in that field would be turned into a proxy, while the rest of the class is serialized.)
Is there something I can do that will allow me to pass the class as serialized, but with the delegate turned into a proxy, just as it would be if it were a MarshalByRefObject
? (Preferably in the setup of the AppDomain so that I don’t need to change MyClass
, but suggestions that involve changing the class are welcome too as long as I don’t need to change the code that creates the delegate.)