Esempio n. 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);
        }
Esempio n. 2
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);
            }
        }