在PowerShell中V2编译新型 - 曲奇意识到Web客户端 [英] Compiling new type in PowerShell v2 - Cookie Aware WebClient
问题描述
我试图编译的<一个是饼干意识到版 HREF =http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx> WebClient的类 - 但我似乎无法克服的一些使用的障碍在PowerShell中V2加入添加型cmdlet的。这里是我试图编译的代码:
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.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类型不能为发现(虽然它是完全合格的...) - 显然我的东西盲目
It can't seem to find the CookieContainer type can't be found (though it is fully qualified...) - clearly I am blind on something.
编辑:更新示例代码是正确的。复制正pasteable,谢谢!
Updated the sample code to be correct and copy-n-pasteable, thanks!
推荐答案
第二个参照的CookieContainer
通过构造函数表达式是完全合格的。第一个参考,声明字段时, m_container
不是。使双方完全合格因此Powershell的可以找到他们。
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();
这篇关于在PowerShell中V2编译新型 - 曲奇意识到Web客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!