dimanche 19 juin 2016

Need to find all objects in a list of DLLs that extend a known base class. Also need to be able to unload and reload those dlls

The user will provide a list of DLLs, and I want to go through them and get all objects that extend a specific base class. I need to get these objects and their properties that are marked with a specific metadata. I would use Assembly.Load, but I need the ability to unload and reload the DLLs, since the user can change the list at any time. I just need to know the type names and the properties with the meta data. I don't need to instantiate any instances.

My first attempt was to use ReflectionOnlyLoadFrom. However, the DLLs can have references that are not in the GAC, so this fails.

My current plan is to create a new AppDomain when I load these DLLs. When I'm about to reload them, unload that AppDomain, and create a new one. I've followed this thread to get started: How to Load an Assembly to AppDomain with all references recursively?. However, I think I misunderstand how to use this properly.

I've created my proxy:

 class ProxyDomain : MarshalByRefObject
    {
        public Assembly GetAssembly(string assemblyPath)
        {
            try
            {
                return Assembly.LoadFrom(assemblyPath);
                //If you want to do anything further to that assembly, you need to do it here.
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(ex.Message, ex);
            }
        }
    }

And now to get the Assembly.

AppDomain myAppDomain = AppDomain.CreateDomain("GameEnvironment", AppDomain.CurrentDomain.Evidence);

Type type = typeof (ProxyDomain);

foreach(string dll in listOfDlls)
{
    ProxyDomain proxy = (ProxyDomain)myAppDomain.CreateInstanceFromAndUnwrap(dll, type.FullName);
    Assembly asm = proxy.GetAssembly(dlls);
    //TODO: Now get the objects out of it
}

The problem with this is it's trying to load ProxyDomain from the DLL. ProxyDomain exists in my current executable, not in the DLL. I don't know any of the types in the DLL. How can I load the DLLs into an AppDomain without knowing what's in it?





Aucun commentaire:

Enregistrer un commentaire