开发者

Compiling new type in PowerShell v2 - Cookie Aware WebClient

开发者 https://www.devze.com 2023-01-26 20:38 出处:网络
I am trying to compile a \"cookie aware\" version of the WebClient class - but I can\'t seem to get over some of the hurdles of using the Add-Type cmdlet added in PowerShell v2. Here is the code I am

I am trying to compile a "cookie aware" version of the WebClient class - but I can't seem to get over some of the hurdles of using the Add-Type cmdlet added in PowerShell v2. Here is the code I am trying to compile:

PS C:\> $def = @"
public class CookieAwareWebClient : System.Net.WebClient
{
  private System.Net.Co开发者_Go百科okieContainer 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

It can't seem to find the CookieContainer type can't be found (though it is fully qualified...) - clearly I am blind on something.

Edit: Updated the sample code to be correct and copy-n-pasteable, thanks!


The second reference to CookieContainer with the constructor expression is fully qualified. The first reference, when declaring the field m_container is not. Make both fully qualified so Powershell can find them

private System.Net.CookieContainer m_container = new System.Net.CookieContainer();
0

精彩评论

暂无评论...
验证码 换一张
取 消