如何重载 PowerShell 内置类的方法

编程入门 行业动态 更新时间:2024-10-23 15:25:27
本文介绍了如何重载 PowerShell 内置类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以重载 PowerShell 内置类的方法吗?如果是,那么如何?任何代码示例都很棒.

Can I overload the PowerShell inbuilt class's methods? If yes then how? Any code sample would be great.

本质上,我试图重载哈希表字典 PowerShell 对象的 Equals 方法以进行适当的比较.

Essentially, I am trying to overload the Equals method of a Hashtable Dictionary PowerShell object to do appropriate comparisons.

我知道您可以在 PowerShell 中重载 cmdlet 或函数,以及采用的最新定义.但我试图重载不是 cmdlet 或函数,而是试图重载现有类的内置方法.我已经检查过这篇文章:PowerShell 中的函数重载

I know that you can overload a cmdlet or a function in PowerShell and whatever is the latest definition that is taken up. But I am trying to overload not a cmdlet or a function, but trying to overload inbuilt method for existing class. I have already checked this post: Function overloading in PowerShell

推荐答案

好吧,您可以通过以下方式从 C# 代码中重载它:

Well, you can overload it from C# code in this manner:

$Source = @" class MyHashTable : HashTable { public override Equals(...) { ... } } "@ Add-Type -TypeDefinition $Source -Language CSharp $x = New-Object MyHashTable ...

更多推荐

如何重载 PowerShell 内置类的方法

本文发布于:2023-06-11 23:30:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/645285.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!