There are many different ways to create objects in Powershell. I am posting this as a challenge to create objects purely in Powerhell. I am looking for a way to create objects using Add-Type that pass these requirements:
- Strongly-Typed object properties
- Instantiated, using:
New-Object,[My.Object]::New(),[My.Object]@{'Property'='Value'} - Part of a custom Namespace (i.e [My.Object])
- Can be type-checked. Example:
$myVar -is [My.Object]
I know dll's can be created in Visual Studio that would accomplish this, but I am looking for a purely Powershell way to create objects.
Here is the closest example I have that satisfy the rules above:
PS C:\> $memberDef = @"
public String myString { get; set; }
public int myInt { get; set; }
"@
PS C:\> Add-Type -Namespace "My.Namespace" -Name "Object" -MemberDefinition $memberDef
PS C:\> $myObj = [My.Namespace.Object]@{'myString'='Foo'; 'myInt'=42}
PS C:\> $myObj
myString myInt
-------- -----
Foo 42
With that said, are there other (Possibly better) ways to create objects that look, feel, and act like native Powershell objects?
Examples that do not satisfy all rules
Weakly-Typed object properties:
PS C:\> $myObj = "" | Select-Object -Property myString, myInt
PS C:\> $myObj = New-Object -TypeName PSObject -Property @{'myString'='foo'; 'myInt'=42}
PS C:\> $myObj = @{}; $myObj.myString = 'foo'; $myObj.myInt = 42
PS C:\> $myObj.myInt = "This should result in an error."
Objects without namespaces:
PS C:\> $typeDef = @"
public class myObject {
public string myString { get; set; }
public int myInt { get; set; }
}
"@
PS C:\> Add-Type -TypeDefinition $typeDef
PS C:\> $myObj = [myObject]::new()
PS C:\> $myObj = New-Object -TypeName PSObject -Property @{'myString'='foo'; 'myInt'=42}
PS C:\> $myObj.PSObject.TypeNames.Insert(0, "myObject")