Last active
October 10, 2020 23:20
-
-
Save bjarkirafn/03416e4c6809ed1aa34a33fa96359118 to your computer and use it in GitHub Desktop.
posh:typedata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management | |
$ERROR_ACTION = 'SilentlyContinue' | |
$jaobj = [PSCustomObject]@{ | |
PSTypeName = 'JaHer.Object' | |
Name = 'Bjarki' | |
Language = 'PowerShell' | |
Country = 'Iceland' | |
} | |
$ExtensionMethods = @( | |
@{ | |
TypeName = 'JaHer.Object' | |
MemberType = 'ScriptMethod' | |
MemberName = 'ToHashTable' | |
Value = { | |
$hash = @{} | |
$this.psobject.properties | | |
ForEach-Object { $hash.Add($_.Name, $_.Value) } | |
$hash | |
} | |
ErrorAction = $ERROR_ACTION | |
} | |
) | |
$toHash = @{ | |
TypeName = 'JaHer.Object' | |
MemberType = 'ScriptMethod' | |
MemberName = 'ToHashTable' | |
Value = { | |
$hash = @{} | |
$this.psobject.properties | | |
ForEach-Object { $hash.Add($_.Name, $_.Value) } | |
$hash | |
} | |
ErrorAction = $ERROR_ACTION | |
} | |
Update-TypeData @toHash | |
# $ExtensionMethods | ForEach-Object {Update-TypeData @_} | |
$toUpper = @{ | |
TypeName = 'JaHer.Object' | |
MemberType = 'ScriptProperty' | |
MemberName = 'Upper' | |
Value = { $this.Name.toUpper() } | |
ErrorAction = $ERROR_ACTION | |
} | |
Update-TypeData @toUpper | |
$TypeData = @{ | |
TypeName = 'JaHer.Object' | |
DefaultDisplayPropertySet = 'Name', 'Language' | |
ErrorAction = $ERROR_ACTION | |
} | |
Update-TypeData @TypeData | |
$jaobj.upper | |
$jaobj.UpperCaseName | |
$jaobj.ToHashTable() | |
function Set-JaObj { | |
param( [PSTypeName('JaHer.Object')]$Data ) | |
Write-Host $Data | |
} | |
function New-JaObj { | |
[OutputType('JaHer.Object')] | |
[CmdletBinding()] | |
param([Hashtable]$Data) | |
$obj = [PSCustomObject]@{ | |
TypeName = 'JaHer.Object' | |
} | |
$Data.GetEnumerator() | | |
ForEach-Object { | |
$obj | Add-Member $_.Key $_.Value | |
} | |
return $obj | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ERROR_ACTION = "SilentlyContinue" | |
$typedata =@{ | |
TypeName = 'System.DateTime' | |
MemberType = 'ScriptMethod' | |
MemberName = 'DaysInThisMonth' | |
Value = { | |
} | |
ErrorAction = $ERROR_ACTION | |
} | |
$Signature = @" | |
public class DateTimeExt | |
{ | |
public static int DaysInThisMonth(this System.DateTime date) | |
=> date.DaysInMonth(date.Now.Year, date.Now.Month) | |
} | |
"@ | |
Add-Type -TypeDefinition $Signature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment