Beispiel #1
0
        public Assembly Find(AssemblyName name)
        {
            if (name.FullName == EmbeddedAssemblyFinder.MonoCecilAssembly.FullName)
            {
                return(EmbeddedAssemblyFinder.MonoCecilAssembly);
            }

            var loadedAssemblies = this.AppDomain.GetAssemblies();

            foreach (var loadedAssembly in loadedAssemblies)
            {
                var embeddedResourcesBytes = EmbeddedAssemblyFinder.GetEmbeddedResourcesBytes(loadedAssembly);

                foreach (var embeddedResourceBytes in embeddedResourcesBytes)
                {
                    var embeddedAssemblyBytes = EmbeddedAssemblyFinder.GetEmbeddedAssemblyBytes(embeddedResourceBytes, name);

                    if (embeddedAssemblyBytes != null)
                    {
                        return(Assembly.Load(embeddedAssemblyBytes));
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
 private static Assembly LoadMonoCecil()
 {
     using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Mono.Cecil.dll"))
     {
         return(Assembly.Load(EmbeddedAssemblyFinder.StreamToByteArray(stream)));
     }
 }
Beispiel #3
0
        private static bool IsEmbeddedResource(Assembly assembly, string resourceName)
        {
            var resourceLocation = assembly.GetManifestResourceInfo(resourceName).ResourceLocation;

            return(EmbeddedAssemblyFinder.HasFlag(ResourceLocation.Embedded, resourceLocation) &&
                   EmbeddedAssemblyFinder.HasFlag(ResourceLocation.ContainedInManifestFile, resourceLocation));
        }
Beispiel #4
0
 private static IEnumerable <byte[]> GetEmbeddedResourcesBytes(Assembly assembly)
 {
     foreach (var resourceName in assembly.GetManifestResourceNames())
     {
         if (EmbeddedAssemblyFinder.IsEmbeddedResource(assembly, resourceName))
         {
             using (var resourceStream = assembly.GetManifestResourceStream(resourceName))
             {
                 yield return(EmbeddedAssemblyFinder.StreamToByteArray(resourceStream));
             }
         }
     }
 }
Beispiel #5
0
        private static byte[] GetEmbeddedAssemblyBytes(byte[] bytes, AssemblyName name)
        {
            using (var stream = new MemoryStream(bytes))
            {
                try
                {
                    var assemblyDefinition = AssemblyDefinition.ReadAssembly(stream);

                    if (assemblyDefinition.FullName == name.FullName)
                    {
                        return(bytes);
                    }

                    foreach (var moduleDefinition in assemblyDefinition.Modules)
                    {
                        foreach (var resource in moduleDefinition.Resources)
                        {
                            if (resource.ResourceType == ResourceType.Embedded)
                            {
                                var embeddedResource = (EmbeddedResource)resource;
                                {
                                    var embeddedResourceBytes = embeddedResource.GetResourceData();

                                    var embeddedAssemblyBytes = EmbeddedAssemblyFinder.GetEmbeddedAssemblyBytes(embeddedResourceBytes, name);

                                    if (embeddedAssemblyBytes != null)
                                    {
                                        return(embeddedResourceBytes);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (BadImageFormatException)
                {
                }

                return(null);
            }
        }
        private static Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
        {
            var finder = new EmbeddedAssemblyFinder(sender as AppDomain);

            return finder.Find(new AssemblyName(args.Name));
        }
Beispiel #7
0
 static EmbeddedAssemblyFinder()
 {
     EmbeddedAssemblyFinder.MonoCecilAssembly = EmbeddedAssemblyFinder.LoadMonoCecil();
 }
        private static Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
        {
            var finder = new EmbeddedAssemblyFinder(sender as AppDomain);

            return(finder.Find(new AssemblyName(args.Name)));
        }