从 powershell 访问私有成员 [英] Accessing private members from powershell

查看:108
本文介绍了从 powershell 访问私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 Uri 停止编码/"正如这里所解释的:获取带有 url 编码斜杠的 URL

I'm trying to get Uri to stop encoding '/' As explained here: GETting a URL with an url-encoded slash

但是如何在 powershell 中实现相同的功能?

But how to achieve the same in powershell ?

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

I'm trying to follow the route of accessing private property and changing it's value but I can't get it to work.

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

[Uri].GetProperties([System.Reflection.BindingFlags]::NonPublic) - returns nothing

有什么想法吗?

推荐答案

这是 PowerShell 的有效解决方案:

This is working solution for 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 路径 :-) 请注意,与 .net 2.0 有一些不同,我的代码适用于 .>0.对于 <= 2.0 版本,flagsValue 对象的类型将是 [Int32] 而不是 [Uint64])

Thanks to google-api-dotnet-client path :-) please note, that there is some difference with .net 2.0, my code is working for > .net 2.0 (for <= 2.0 versions, the type of flagsValue object will be [Int32] instead of [Uint64])

这篇关于从 powershell 访问私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆