知道DLL正在运行的上下文

The*_*iot 1 .net c# dll

我想知道DLL是否在Web或桌面环境中使用.一种方法是检查是否HttpContext为null.但我想知道是否有其他更好的方法来做到这一点.

Fli*_*ter 5

我们经历了同样的事情,因为我们有一个在Windows和Web应用程序中运行的.DLL,并且您已经确定了确定哪个是哪个.

public bool IsWebApp()
{
    return (HttpContext.Current != null);
}
Run Code Online (Sandbox Code Playgroud)

然后在您的应用程序中,您只需查询:

if ( this.IsWebApp() )
{
    //do webby stuff...
}
Run Code Online (Sandbox Code Playgroud)