Get-Field by Andrew Savinykh 33 months ago (modification of post by Andrew Savinykh view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2067"></script>download | new post
Displays private fields of passed object. This function is useful only if you want to want to see what is under the hood in powershell
- function Get-Field{
- [CmdletBinding()]
- param (
- [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
- $InputObject,
- [Parameter(Position=1,Mandatory=$false)]
- [string]$Name,
- [switch]$Force,
- [switch]$AsHashtable
- )
- $publicNonPublic = [Reflection.BindingFlags]::Public -bor [Reflection.BindingFlags]::NonPublic
- $instance = $publicNonPublic -bor [Reflection.BindingFlags]::Instance
- $getField = $instance -bor [Reflection.BindingFlags]::GetField
- $type = $InputObject.gettype()
- $result = @()
- while ($type -ne [object] -and $type -ne [MarshalByRefObject] ) {
- $fields = $type.GetFields($instance)
- $result += $fields | Select-Object @{n="Name";e={$_.Name}},@{n="Value";e={$type.InvokeMember($_.Name, $getField, $null, $InputObject, $null)}}
- $type = $type.BaseType
- if (!$Force) { break; }
- }
- if (!$Name) {
- if ($AsHashtable) {
- $hash = @{}
- $result | Foreach-Object {
- $hash[$_.Name] = $_.Value
- }
- return $hash
- }
- else {
- return $result
- }
- }
- $result | ForEach-Object { if ($_.Name -eq $Name) { $_.Value } }
- }
- New-Alias -Name gf -Value Get-Field -Force
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository