gpt4 book ai didi

winforms - 您如何覆盖Powershell中的功能

转载 作者:行者123 更新时间:2023-12-04 17:32:37 24 4
gpt4 key购买 nike

我在网上可以找到的所有关于此操作的信息都是覆盖cmdlet,但是我需要覆盖Windows窗体事件。这是我需要的C#等效项:

protected override Point ScrollToControl(Control activeControl)
{
return this.AutoScrollPosition;
}

他们说您只是在相同范围内使用该函数的名称,它将自动覆盖它,但这对我不起作用。

最佳答案

If you're referring to a method, Chris Dent's answer covers that nicely!

对于 native PowerShell cmdlet/功能,我将在此处复制my answer from ServerFault:

是的,您可以在Powershell中覆盖Get-ChildItem或任何其他cmdlet。

命名您的功能相同

如果在相同范围内使用相同名称创建函数,则将使用您的函数。

例子:

Function Get-ChildItem {
[CmdletBinding()]
param(
# Simulate the parameters here
)
# ... do stuff
}

使用别名

创建您自己的函数,然后为该函数创建一个别名,该别名与您要覆盖的cmdlet的名称相同。

例子:

Function My-GetChildItem {
[CmdletBinding()]
param(
# Simulate the parameters here
)
# ... do stuff
}

New-Alias -Name 'Get-ChildItem' -Value 'My-GetChildItem' -Scope Global

这种方法很不错,因为在不踩踏内置函数的情况下可以更轻松地测试函数,并且可以控制cmdlet何时被覆盖或不在代码中。

删除别名:

Remove-Item 'Alias:\Get-ChildItem' -Force

知道命令优先级

about_Command_Precedence列出了解释不同类型的命令的顺序:

If you do not specify a path, Windows PowerShell uses the following precedence order when it runs commands:

  1. Alias
  2. Function
  3. Cmdlet
  4. Native Windows commands

关于winforms - 您如何覆盖Powershell中的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com