I am trying to customize the assembly resolution process by wrapping the AppDomain and its AssemblyResolve event inside a class. The simplified version of my ClassLoader is below. The problem I am having is that when the event AssemblyResolve is fired, it seems that I get a new instance of ClassLoader, not the one I previously created.
[Serializable]
public class ClassLoader // : IDisposable
{
public AppDomain Domain { get; private set; }
public string FooProperty { get; set; }
public ClassLoader(string domain) {
Domain = AppDomain.CreateDomain(domain);
Domain.AssemblyResolve += Domain_AssemblyResolve;
}
private Assembly Domain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine(
"ClassLoader HashCode: {0} FooProperty: {1}\n\n",
GetHashCode(),
FooProperty);
// ...
return null;
}
// ...
}
When executing this code, FooProperty is not initialized in the Domain_AssemblyResolve event handler and the ClassLoader instance has a different hash code from “c”.
var c = new ClassLoader("demo");
c.FooProperty = "Foo";
Console.WriteLine(
"c Hash Code: {0} FooProperty: {1}",
c.GetHashCode(),
c.FooProperty);
c.Domain.CreateInstanceAndUnwrap("Not important", "Not important");
Do you what is happening? or some workaround?
Thanks!
The
ClassLoaderinstancecis created on application domainAand since it does not inherits fromMarshalByRefObjectit will be serialized into the application domain that you are creating at the moment you add the event handler toAssemblyResolve. This happens because the method is an instance method and the delegate will need a reference to the target object where the method will be called.If you just want that the
FooPropertyhave a specific value when the event handler is triggered then you can add the event handler only after initializing the property causing the value to be serialized and available in the newly created application domain.If you have the requirement that the instance
cis available in both application domains then you should take a look at:Making Objects Remotable
Events and Delegates with .NET Framework Remoting