如何从MSIL或.NET PE文件中提取资源内容

时间:2015-05-21 14:02:02

标签: .net portable-executable disassembly ilspy

请检查图片链接,我需要从MSIL文件中提取资源内容。我已经使用ILSpy调试了文件,但我需要以任何其他方式进行调试。没有使用任何手动交互。

http://i.stack.imgur.com/ZQdRc.png

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容执行此操作:

public class LoadAssemblyInfo : MarshalByRefObject
{
    public string AssemblyName { get; set; }

    public Tuple<string, byte[]>[] Streams;

    public void Load()
    {
        Assembly assembly = Assembly.ReflectionOnlyLoad(AssemblyName);

        string[] resources = assembly.GetManifestResourceNames();

        var streams = new List<Tuple<string, byte[]>>();

        foreach (string resource in resources)
        {
            ManifestResourceInfo info = assembly.GetManifestResourceInfo(resource);

            using (var stream = assembly.GetManifestResourceStream(resource))
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);

                streams.Add(Tuple.Create(resource, bytes));
            }
        }

        Streams = streams.ToArray();
    }
}

// Adapted from from http://stackoverflow.com/a/225355/613130
public static Tuple<string, byte[]>[] LoadAssembly(string assemblyName)
{
    LoadAssemblyInfo lai = new LoadAssemblyInfo
    {
        AssemblyName = assemblyName,
    };

    AppDomain tempDomain = null;

    try
    {
        tempDomain = AppDomain.CreateDomain("TemporaryAppDomain");
        tempDomain.DoCallBack(lai.Load);
    }
    finally
    {
        if (tempDomain != null)
        {
            AppDomain.Unload(tempDomain);
        }
    }

    return lai.Streams;
}

使用它像:

var streams = LoadAssembly("EntityFramework");

streamsTuple<string, byte[]>的数组,其中Item1是资源的名称,Item2是资源的二进制内容。

它非常复杂,因为它会在另一个Assembly.ReflectionOnlyLoad中执行AppDomain,然后将其卸载(AppDomain.CreateDomain / AppDomain.Unload)。