从powershell访问私有成员

时间:2012-10-03 13:19:11

标签: powershell

我正试图让Uri停止编码'/' 如下所述: GETting a URL with an url-encoded slash

但是如何在PowerShell中实现同样的目标?

我正在尝试遵循访问私有财产并改变其价值的路线,但我无法让它发挥作用。

[Uri].GetProperties([System.Reflection.BindingFlags]::NonPublic) - 不返回任何内容

有什么想法吗?

4 个答案:

答案 0 :(得分:6)

这是PowerShell的有效解决方案:

$uri = [Uri]"http://example.com/%2F"
# access the .PathAndQuery field to initialize the Uri object
$pathAndQuery = $uri.PathAndQuery
$flagsField = $uri.GetType().GetField("m_Flags", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Instance)
$flagsValue = [UInt64]$flagsField.GetValue($uri)
# remove flags Flags.PathNotCanonical and Flags.QueryNotCanonical
$flagsValue = [UInt64]($flagsValue -band (-bnot 0x30));
$flagsField.SetValue($uri, $flagsValue)
Write-Host $uri.AbsoluteUri

感谢google-api-dotnet-client path :-)请注意,与.net 2.0存在一些差异,我的代码适用于> .net 2.0(对于< = 2.0版本,flagsValue对象的类型将是[Int32]而不是[Uint64]

答案 1 :(得分:1)

将其他帖子视为您需要的内容,您需要:

$uri = [uri]"http://example.com/%2F"
$f = [uri].getfield("m_Flags", "nonpublic,instance")
$v = [int]($f.getvalue($uri))
$f.setvalue($uri, [uint64]($v -band (-bnot 0x30)))

PowerShell的-bnot-band按位运算符不适用于大于[int]的任何类型,因此我向下转换为[int],但上述情况不会溢出(这意味着超出[int]::maxvalue的标志值不存在。)

答案 2 :(得分:0)

试试这个(就像你的链接一样):

$uri.GetType().GetField("m_Flags", [System.Reflection.BindingFlags]::Instance -bor `
 [System.Reflection.BindingFlags]::NonPublic)

获取非公共属性:

$uri.GetType().GetProperties( [System.Reflection.BindingFlags]::Instance -bor `
[System.Reflection.BindingFlags]::NonPublic )

答案 3 :(得分:0)

$uri.GetType().GetProperties('Instance,NonPublic')