c# - MEF - Get assembly from embedded DLL -


i using mef create "plugins" wpf application. of these plugins want embed directly exe file exe needs standalone. using costura fody embed resource along other references. exe file needs standalone unable create directory these plugins , use directoycatalog

is there anyway can either load assembly embedded resource, or specify assembly name such as:

catalog.catalogs.add(new assemblycatalog("my.assembly.name)); 

i have tried looping through manifest resources these appear zipped fody:

var resourcenames = gettype().assembly.getmanifestresourcenames();             foreach (var resourcename in resourcenames) 

any help/suggestions appreciated.

ok got work me, using class below (found code @ https://github.com/sebazzz/entityprofiler/blob/master/src/ui/entityprofiler.viewer/appbootstrapper.cs , tweaked suit needs):

to use call extract function find costura zip files in resource manifest , decompresses , registers it.

the function returns dictionary of assemblies match string passed in function. iterate on them , add catalog used 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);     } } 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -