我正在使用MEF为我的WPF应用程序创建“插件”。我想将这些插件中的一些直接嵌入到EXE文件中,因为EXE需要是独立的。我使用Fody的Costura将资源与我的所有其他参考资料一起嵌入。由于exe文件需要独立,我无法为这些插件创建目录并使用DirectoyCatalog
无论如何我可以从嵌入式资源加载程序集,或者只是指定程序集名称,例如:
catalog.Catalogs.Add(new AssemblyCatalog("My.Assembly.Name));
我已尝试循环显示Manifest资源,但这些资源似乎是由Fody压缩的:
var resourceNames = GetType().Assembly.GetManifestResourceNames();
foreach (var resourceName in resourceNames)
任何帮助/建议表示赞赏。
答案 0 :(得分:4)
好的,所以我可以使用下面的课程(在https://github.com/Sebazzz/EntityProfiler/blob/master/src/UI/EntityProfiler.Viewer/AppBootstrapper.cs找到此代码并调整以满足我的需求):
要使用它,您只需调用提取函数,该函数将在资源清单中找到任何Costura Zip文件并对其进行解压缩并进行注册。
该函数返回与函数中传递的字符串匹配的所有程序集的字典。然后我迭代它们并添加到目录以供合成容器使用:
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);
类别:
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);
}
}
答案 1 :(得分:0)
仅通过从当前AppDomain加载程序集,我就可以在项目中使用它。
opts.secretOrKey = keys.secretOrKey