HttpContext 有一个带有两个重载的公共构造函数,但它不是默认的(无参数)一个。
例如,您需要传入一个 SimpleWorkerRequest 实例以实例化一个 HttpContext 实例并将其分配给 HttpContext.Current:
//Initialize this stuff with some crap
string appVirtualDir = "/";
string appPhysicalDir = @"C:\Documents and Settings\";
string page = @"localhost";
string query = string.Empty;
TextWriter output = null;
//Create a SimpleWorkerRequest object passing down the crap
SimpleWorkerRequest workerRequest = new SimpleWorkerRequest(appVirtualDir, appPhysicalDir, page, query, output);
//Create your fake HttpContext instance
HttpContext.Current = new HttpContext(workerRequest);
有关详细信息,请参阅此链接。
无论如何,有些类没有公共构造函数 - 例如,考虑一个单例类,构造函数是私有的(您可以调用静态 getInstance 方法来获取当前实例,或者如果它为空则创建它)。
干杯