Skip to content

Instantly share code, notes, and snippets.

@kdoblosky
Created February 27, 2014 07:17
Show Gist options
  • Save kdoblosky/9245780 to your computer and use it in GitHub Desktop.
Save kdoblosky/9245780 to your computer and use it in GitHub Desktop.
Creating Objects With Methods in PowerShell - Part 3
# Code from my blog entry at http://betterlivingthroughcode.blogspot.com/2014/02/creating-objects-with-methods-in_27.html
$myObject -is [MyType]
$myObject.PSObject.TypeNames.Insert(0, "MyType")
$myObject.PSObject.TypeNames[0] -eq "MyType"
$myObject | Add-Member -MemberType ScriptMethod -Name MyMethod -Value { param([string]$p1, [int]$p2) $p1 * $p2 }
$myObject | Get-Member
$code = @'
public class MyClass {
public string MyProperty1 = "Default";
public int MyProperty2 = 23;
public string MyMethod(string P1, int P2) {
string results = "";
for (int i = 1; i <= P2; i++) {
results += P1;
}
return results;
}
}
'@
Add-Type -TypeDefinition $code -Language CSharp
$myObject = New-Object -TypeName MyClass
$myObject | Get-Member
$myObject -is [MyClass]
Function NewAddressUsingAddMember() {
$Address = [pscustomobject]@{
Street1 = "123 Anywhere Lane";
Street2 = "Apartment 42";
City = "Fnord";
State = "AA";
Zip = "55555"
}
$Address | Add-Member -MemberType ScriptMethod -Name WriteAddress -Value { "$($this.Street1)`n$($this.Street2)`n$($this.City), $($this.State) $($this.Zip)" }
$Address
}
Function NewAddressUsingNewModule() {
$Address = New-Module -ScriptBlock {
[string]$Street1 = "123 Anywhere Lane"
[string]$Street2 = "Apartment 42"
[string]$City = "Fnord"
[string]$State = "AA"
[string]$Zip = "55555"
Function WriteAddress() { "$($this.Street1)`n$($this.Street2)`n$($this.City), $($this.State) $($this.Zip)" }
Export-ModuleMember -Function * -Variable *
} -AsCustomObject
$Address
}
Function AddAddressType() {
$code = @"
public class Address {
public string Street1 = "123 Anywhere Lane";
public string Street2 = "Apartment 42";
public string City = "Fnord";
public string State = "AA";
public string Zip = "55555";
public string WriteAddress() {
return string.Format("{0}\n{1}\n{2}, {3} {4}", Street1, Street2, City, State, Zip);
}
}
"@
Add-Type -TypeDefinition $code -Language CSharp
}
Function NewAddressUsingAddType() {
AddAddressType
New-Object -TypeName Address
}
Function MeasureIt($CommandName, $Count, $ScriptBlock) {
$ms = Measure-Command -Expression $ScriptBlock | Select-Object -ExpandProperty TotalMilliseconds
"Using $CommandName $Count times took $ms milliseconds"
}
# Measure each once
MeasureIt "Add-Member" 1 { NewAddressUsingAddMember }
MeasureIt "New-Module" 1 { NewAddressUsingAddMember }
MeasureIt "Add-Type" 1 { NewAddressUsingAddType }
# Measure each method 1000 times
MeasureIt "Add-Member" 10000 { 1..10000 | % { NewAddressUsingAddMember } }
MeasureIt "New-Module" 10000 { 1..10000 | % { NewAddressUsingAddMember } }
MeasureIt "Add-Type" 10000 { 1..10000 | % { NewAddressUsingAddType } }
Function NewAddressUsingAddTypeWithoutInitializing() {
New-Object -TypeName Address
}
# Measure each method 1000 times
MeasureIt "Add-Member" 1000 { 1..1000 | % { NewAddressUsingAddMember } }
MeasureIt "New-Module" 1000 { 1..1000 | % { NewAddressUsingAddMember } }
MeasureIt "Add-Type" 1000 { 1..1000 | % { NewAddressUsingAddType } }
MeasureIt "Add-Type" 1000 { 1..1000 | % -Begin { AddAddressType } -Process { NewAddressUsingAddTypeWithoutInitializing } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment