MEF - Get assembly from embedded DLL

隐身守侯 提交于 2019-12-20 01:11:48

问题


I am using MEF to create "plugins" for my WPF application. Some of these plugins I want to embed directly into the EXE file as the EXE needs to be standalone. I am using Costura by Fody to embed the resource along with all my other references. As the exe file needs to be standalone I am unable to create a directory for these plugins and use the DirectoyCatalog

Is there anyway I can either load the assembly from the embedded resource, or simply specify the assembly name such as:

catalog.Catalogs.Add(new AssemblyCatalog("My.Assembly.Name));

I have tried looping through the Manifest resources but these appear to be zipped by Fody:

var resourceNames = GetType().Assembly.GetManifestResourceNames();
            foreach (var resourceName in resourceNames)

Any help/suggestions appreciated.


回答1:


Ok so got this to work for me, using the class below (found this code at https://github.com/Sebazzz/EntityProfiler/blob/master/src/UI/EntityProfiler.Viewer/AppBootstrapper.cs and tweaked to suit my needs):

To use it you simply call the extract function which will find any Costura Zip files in resource manifest and decompresses it and registers it.

The function returns a dictionary of all assemblies that match the string passed in the function. I then iterate over them and add to catalog to be used by composition container:

var assemblies = CosturaAssemblyExtractor.Extract(AppDomain.CurrentDomain, Assembly.GetExecutingAssembly(), "My.AssemblyName");
foreach (var assembly in assemblies)
{
    catalog.Catalogs.Add(new AssemblyCatalog(assembly.Value));
}
container = new CompositionContainer(catalog);

Class:

public static class CosturaAssemblyExtractor
{
    public static Dictionary<string, Assembly> Extract(AppDomain OrigDomain, Assembly ExecutingAssembly, string AssemblyStartsWith)
    {
        //var currentDomain = origDomain;
        var assemblies = OrigDomain.GetAssemblies();

        var references = new Dictionary<string, Assembly>();

        var manifestResourceNames = ExecutingAssembly.GetManifestResourceNames().Where(x => {
            return x.ToUpper().StartsWith(("costura." + AssemblyStartsWith).ToUpper()) && x.ToUpper().EndsWith(".dll.zip".ToUpper());
        });

        foreach (var resourceName in manifestResourceNames)
        {
            var solved = false;
            foreach (var assembly in assemblies)
            {
                var refName = string.Format("costura.{0}.dll.zip", GetDllName(assembly, true));
                if (resourceName.Equals(refName, StringComparison.OrdinalIgnoreCase))
                {
                    references[assembly.FullName] = assembly;
                    solved = true;
                    break;
                }
            }

            if (solved)
                continue;

            using (var resourceStream = ExecutingAssembly.GetManifestResourceStream(resourceName))
            {
                if (resourceStream == null) continue;

                if (resourceName.EndsWith(".dll.zip"))
                {
                    using (var compressStream = new DeflateStream(resourceStream, CompressionMode.Decompress))
                    {
                        var memStream = new MemoryStream();
                        CopyTo(compressStream, memStream);
                        memStream.Position = 0;

                        var rawAssembly = new byte[memStream.Length];
                        memStream.Read(rawAssembly, 0, rawAssembly.Length);
                        var reference = Assembly.Load(rawAssembly);
                        references[reference.FullName] = reference;
                    }
                }
                else
                {
                    var rawAssembly = new byte[resourceStream.Length];
                    resourceStream.Read(rawAssembly, 0, rawAssembly.Length);
                    var reference = Assembly.Load(rawAssembly);
                    references[reference.FullName] = reference;
                }
            }
        }
        return references;
    }

    private static void CopyTo(Stream source, Stream destination)
    {
        var array = new byte[81920];
        int count;
        while ((count = source.Read(array, 0, array.Length)) != 0)
        {
            destination.Write(array, 0, count);
        }
    }

    private static string GetDllName(Assembly assembly, bool withoutExtension = false)
    {
        var assemblyPath = assembly.CodeBase;
        return withoutExtension ? Path.GetFileNameWithoutExtension(assemblyPath) : Path.GetFileName(assemblyPath);
    }
}



回答2:


I was able to get this to work in my project simply by loading the assemblies from the current AppDomain.

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    catalog.Catalogs.Add(new AssemblyCatalog(assembly));
}


来源:https://stackoverflow.com/questions/39368405/mef-get-assembly-from-embedded-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!