Hello,
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!