Willbill
Willbill

Reputation: 4732

Prototypal inheritance in Powershell?

Are there any libraries or projects that implement prototypal inheritance for PSObjects in Powershell?

Upvotes: 4

Views: 865

Answers (2)

fridojet
fridojet

Reputation: 1296

What about$obj = New-Object -TypeName PSObject -ArgumentList $prototype?

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201662

I don't think so since you can't even get a PSObject to identify itself as a custom type e.g.:

PS> $obj = new-object psobject
PS> $obj.psobject.TypeNames.Insert(0,'MyCustomType')
PS> $obj.psobject.TypeNames
MyCustomType
System.Management.Automation.PSCustomObject
System.Object
PS> $obj -is [MyCustomType]
Unable to find type [MyCustomType]: make sure that the assembly 
containing this type is loaded.

However you can use Add-Type -TypeDefinition to sprinkle C#-based class definitions, complete with inheritance, in your script. The thing with PowerShell it is still primarily a command line shell and scripting language. It doesn't have (and probably shouldn't have) all the features of a general purpose programming language like say Python.

Upvotes: 4

Related Questions