Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read properties from DLL before Assembly.LoadFile()

My program have a pluginManager module, it can load a DLL file and run DLL's methods, but I need read the DLL properties before Assembly.LoadFile(). What should I do?

I readed about Assembly documents, they read properties after Assembly.LoadFile(), you know Assembly no UnLoad() Method, so I must read properties before LoadFile() enter image description here

    private void ImprotZip(string path)
    {
        /*
        1、create tempDir, uppackage to tempDir
        2、Load Plugin DLL, Load plugin dependent lib DLL
        */
        string tempDirectory = CreateRuntimeDirectory(path);
        string[] dllFiles = Directory.GetFiles(tempDirectory);
        ///Load DlL
        foreach(string dll in dllFiles)
        {
            ImprotDll(dll, false);
        }
        string libPath = string.Format("{0}\\lib\\", tempDirectory);
        if (!Directory.Exists(libPath))
            return;
        string[] dlls = Directory.GetFiles(libPath);
        ///Load plugin dependent lib DLL
        foreach(string dll in dlls)
        {
            try
            {
                //filtering same DLL 
                //if(Dll.properties.AssemblyProduct != "something")
                //continue;
                Assembly.LoadFile(dll);
            }
            catch(Exception e)
            {
                e.Log();
            }
        }
    }
like image 289
Zigit Ho Avatar asked Dec 15 '15 02:12

Zigit Ho


2 Answers

You can load assemblies into a reflection-only context using Assembly.ReflectionOnlyLoad.

I would still create an app domain for the reflection only context so you can unload the domain when you are done. You can create app domains by using the AppDomain.CreateDomain method. You should be doing this for plugins anyway so you can unload them when you are done.

like image 113
Ron Beyer Avatar answered Nov 17 '22 20:11

Ron Beyer


FileVersionInfo.GetVersionInfo is exactly what you're looking for.

like image 24
Michael Gunter Avatar answered Nov 17 '22 20:11

Michael Gunter