Powershell 中的原型继承?

发布于 2024-09-17 19:53:28 字数 50 浏览 2 评论 0原文

是否有任何库或项目可以在 Powershell 中实现 PSObject 的原型继承?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-09-24 19:53:28

我不这么认为,因为您甚至无法让 PSObject 将自身标识为自定义类型,例如:

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.

但是您可以使用 Add-Type -TypeDefinition 来添加基于 C# 的类定义,并完成继承,在你的脚本中。 PowerShell 的本质仍然是一种命令行 shell 和脚本语言。它不具备(也可能不应该具备)通用编程语言(例如 Python)的所有功能。

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.

黯淡〆 2024-09-24 19:53:28

$obj = New-Object -TypeName PSObject -ArgumentList $prototype 怎么样?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文