Use the [Serializable] attribute or subclassing from MarshalByRefObject?
- by Theo Lenndorff
I'd like to use an object across AppDomains.
For this I can use the [Serializeable] attribute:
[Serializable]
class MyClass
{
public string GetSomeString() { return "someString" }
}
Or subclass from MarshalByRefObject:
class MyClass: MarshalByRefObject
{
public string GetSomeString() { return "someString" }
}
In both cases I can use the class like this:
AppDomain appDomain = AppDomain.CreateDomain("AppDomain");
MyClass myObject = (MyClass)appDomain.CreateInstanceAndUnwrap(
typeof(MyClass).Assembly.FullName,
typeof(MyClass).FullName);
Console.WriteLine(myObject.GetSomeString());
Why do both approaches seem to have the same effect? What is the difference in both approaches? When should I favor the one approach over the other?
EDIT: At the surface I know that there are differences between both mechanisms, but if someone jumped out of a bush and asked me the question I couldn't give him a proper answer. The questions are quite open questions. I hoped that someone can explain it better than I could do.