How to overload the PowerShell inbuilt class's methods

偶尔善良 提交于 2019-12-06 11:38:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!