问题
Can I overload the PowerShell inbuilt class's methods? If yes then how? Any code sample would be great.
Essentially, I am trying to overload the Equals method of a Hashtable Dictionary PowerShell object to do appropriate comparisons.
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
回答1:
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 ...
来源:https://stackoverflow.com/questions/36749906/how-to-overload-the-powershell-inbuilt-classs-methods