请检查图片链接,我需要从 MSIL 文件中提取资源内容。我已经使用 ILSpy 调试了该文件,但我需要以任何其他方式进行调试。不使用任何人工干预。
问问题
245 次
1 回答
1
你可以这样做:
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");
streams
是 的数组Tuple<string, byte[]>
,其中Item1
是资源的名称,是资源Item2
的二进制内容。
它非常复杂,因为它Assembly.ReflectionOnlyLoad
在另一个AppDomain
然后被卸载 ( AppDomain.CreateDomain
/ AppDomain.Unload
) 中执行。
于 2015-05-21T14:27:15.230 回答