我正在尝试编译一个“cookie aware”版本的WebClient类,但是我似乎无法克服在PowerShell v2中使用Add-Type cmdlet时遇到的一些障碍。以下是我正在尝试编译的代码:
PS C:\> $def = @"
public class CookieAwareWebClient : System.Net.WebClient
{
private System.Net.CookieContainer m_container = new System.Net.CookieContainer();
protected override System.Net.WebRequest GetWebRequest(System.Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request is System.Net.HttpWebRequest)
{
(request as System.Net.HttpWebRequest).CookieContainer = m_container;
}
return request;
}
}
"@
PS C:\> Add-Type -TypeDefinition $def
它似乎找不到CookieContainer类型(尽管它已经完全限定...)-显然我在某些地方出了问题。