HashTables to build an object.

You probably know already v2 feature that allows you to pass hashtable to New-Object cmdlet and have key-value pairs changed into property names – values. I love this feature so much that I usually try to write code using it as much as I can. E.g. description of windows.forms.button may look like this:

$button = New-Object Windows.Forms.Button            
$button.Text = 'OK'            
$size = New-Object Drawing.Size            
$size.Height = 200            
$size.Width = 20            
$button.size = $size

And that works fine, but I avoid this type of syntax. My version would look like that:

$Mybutton = New-Object Windows.Forms.Button -Property @{            
    Text = 'OK'            
    Size = New-Object Drawing.Size -Property @{            
        Height = 200            
        Width = 20 }            
    }

Beauty is in an eye of beholder so I won’t convince you mine is better. But I guess it is clear now, that I simply love this v2 way of adding/ defining properties of an object. So when I saw few times question about recursive object creation using hashtables I decided I have to give it a try. So here is my version that creates multi-level PSObject:

function New-ObjectRecursive {            
    param(            
        $Property            
        )            
    if ($Property -is [hashtable]) {             
        $PropertyHash = @{}            
        foreach ($key in $Property.keys) {            
            $PropertyHash.Add($key, $(New-ObjectRecursive $Property[$key]))            
        }            
        New-Object PSObject -Property $PropertyHash            
    } else {            
        $Property            
    }            
}

Now – what I would like to do is somehow ‘inject’ object type. Maybe I will find a way to have something like:

$MyButton = New-ObjectRecurse @{ Type = 'Windows.Forms.Button';            
    Text = 'OK'            
    Size = @{Type = 'Drawing.Size'; Width = 200; Height = 20 }            
    Location = @{ Type = 'Drawing.Point'; X = 10; Y = 10 }            
    }

For now I can only create stuff like that:

$obj = New-ObjectRecursive -Property @{ Alfa = 1; Beta = @{ One = 1; Two = 2; Three = @{ Deep = 'Foo'; Very = 'Sweet' } } }            
$obj.Beta.Three.Deep
Foo

But somebody asked for it, so maybe he has some use case for that… I hope. 😉

So I did it! 🙂

EDIT: soon after I’ve posted I tried to get to the point where I wanted to be. So now you can either give your hashtable a ‘Type’ key to define a type, or skip it and have PSObject (default type):

function New-ObjectRecursive {            
    param(            
        $Property            
        )            
    if ($Property -is [hashtable]) {            
        if ($Type = $Property.Type) {            
            $Property.Remove('Type')            
        } else {            
            $Type = 'PSObject'            
        }            
        $PropertyHash = @{}            
        foreach ($key in $Property.keys) {            
            $PropertyHash.Add($key, $(New-ObjectRecursive $Property[$key]))            
        }            
        New-Object $Type -Property $PropertyHash            
    } else {            
        $Property            
    }            
}            

Enjoy! 😀

1 thought on “HashTables to build an object.

Leave a comment