如何重载 PowerShell 内置类的方法 [英] How to overload the PowerShell inbuilt class's methods

查看:43
本文介绍了如何重载 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 内置类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆