0
votes

I need to load several .dll files in a separate AppDomain, do some operations with them and unload the AppDomain. I can do that through CreateInstanseFrom but I need to know the name of the type.

If I get all the types in the given assembly, I can filter out mine.

I can get all the types through reflection, but that only works for the current AppDomain, right? It's no use loading the files first in the current domain, get the types and load them into the custom domain.

Is there a method to load an assembly from a file to a custom app domain?

1
I've dropped my answer because I'm out of time to assist you in finding the best approach right now... Sorry.Matías Fidemraizer

1 Answers

1
votes

Instead of trying to use a class from one of the target assemblies in the CreateInstanceFrom/CreateInstanceFromAndUnwrap call, use a class of your own. You can create that well-known class inside the appdomain and call to a well-known method. Inside that well-known method, process the assemblies.

// This class will be created inside your temporary appdomain.
class MyClass : MarshalByRefObject
{
    // This call will be executed inside your temporary appdomain.
    void ProcessAssemblies(string[] assemblyPaths)
    {
        // the assemblies are processed here
        foreach (var assemblyPath in assemblyPaths)
        {
            var asm = Assembly.LoadFrom(assemblyPath);
            ...
        }
    }
}

And use it like so to process the assemblies:

string[] assembliesToProcess = ...;

// create the temporary appdomain
var appDomain = AppDomain.CreateDomain(...);
try
{
    // create a MyClass instance within the temporary appdomain
    var o = (MyClass) appDomain.CreateInstanceFromAndUnwrap(
        typeof(MyClass).Assembly.Location,
        typeof(MyClass).FullName);

    // call into the temporary appdomain to process the assemblies
    o.ProcessAssemblies(assembliesToProcess);
}
finally
{
    // unload the temporary appdomain
    AppDomain.Unload(appDomain);
}