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);
}