views:

38

answers:

2

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!

A: 

Well,

When executing this code, FooProperty is not initialized in the Domain_AssemblyResolve event

There doesn't seem to be any code that initialises Foo inside the handler.

the ClassLoader instance has a different hash code from "c".

Instances of classes that inherit from Object (like ClassLoader) and don't reimplement GetHashCode(), like in your case, will all have different hash codes (based on the address of the object). Override GetHashCode() if you want consistent codes based on internal state.

Mau
A: 

The ClassLoader instance c is created on application domain A and since it does not inherits from MarshalByRefObject it will be serialized into the application domain that you are creating at the moment you add the event handler to AssemblyResolve. 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 FooProperty have 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.

public string FooProperty { get; private set; }

public ClassLoader(string domain, string fooProperty)
{
    FooProperty = fooProperty; // Set it before adding event handler
    Domain = AppDomain.CreateDomain(domain);
    Domain.AssemblyResolve += Domain_AssemblyResolve;
}

If you have the requirement that the instance c is available in both application domains then you should take a look at:

Making Objects Remotable

Events and Delegates with .NET Framework Remoting

João Angelo