Assembly.GetEntryAssembly()不适用于Web应用程序.
但是......我真的需要这样的东西.我使用了一些在Web和非Web应用程序中使用的深层嵌套代码.
我目前的解决方案是浏览StackTrace以找到第一个被调用的程序集.
/// <summary>
/// Version of 'GetEntryAssembly' that works with web applications
/// </summary>
/// <returns>The entry assembly, or the first called assembly in a web application</returns>
public static Assembly GetEntyAssembly()
{
// get the entry assembly
var result = Assembly.GetEntryAssembly();
// if none (ex: web application)
if (result == null)
{
// current method
MethodBase methodCurrent = null;
// number of frames to skip
int framestoSkip = 1;
// loop until we cannot got further in …Run Code Online (Sandbox Code Playgroud)