Accessing private members from powershell

前端 未结 4 1408
日久生厌
日久生厌 2021-01-12 10:39

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

But how to achieve the same in powershell ?

I\'m tr

相关标签:
4条回答
  • 2021-01-12 11:26

    try this (like in your link):

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

    to get non public properties:

    $uri.GetType().GetProperties( [System.Reflection.BindingFlags]::Instance -bor `
    [System.Reflection.BindingFlags]::NonPublic )
    
    0 讨论(0)
  • 2021-01-12 11:28

    Taking that other post as what you need, you want this:

    $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's -bnot and -band bitwise operators don't work with any types bigger than [int] so I'm downcasting to [int] which does not overflow for the above case (which means flag values beyond [int]::maxvalue are not present.)

    0 讨论(0)
  • 2021-01-12 11:35
    $uri.GetType().GetProperties('Instance,NonPublic')
    
    0 讨论(0)
  • 2021-01-12 11:36

    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
    

    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])

    0 讨论(0)
提交回复
热议问题