在Powershell v2中使用扩展方法是否有更简单的方法

时间:2022-09-23 08:09:13

Background

this post explains how one can consume extension methods in Powershell

这篇文章解释了如何在Powershell中使用扩展方法

http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx

Compare this to what someone does in C# - they add a "using SomeAssembly" and all the extension methods are loaded.

将此与C#中的人员进行比较 - 他们添加“使用SomeAssembly”并加载所有扩展方法。

My questions

Did this get simpler Powershell 2.0. And if so, what does one do to use extension methods in Powershell 2.0? I have checked the publically available documentation and installed the CTP and am not seeing anything that is helping.

这是否变得更简单Powershell 2.0。如果是这样,在Powershell 2.0中使用扩展方法会怎么做?我检查了公开可用的文档并安装了CTP,但没有看到任何有用的信息。

1 个解决方案

#1


It doesn't get easier in V2, but there is an extension mechanism which you might not be aware of.

它在V2中并不容易,但有一种你可能不知道的扩展机制。

I believe that part of the problem is in PowerShell's handling (or lack thereof) of generics.

我认为问题的一部分在于PowerShell处理(或缺乏)泛型。

Also, for extension methods to be applied, the typed collections would have to be enforced, which is difficult in PowerShell. PowerShell, as a dynamic language, supports building collections of various types and most collections are represented as arrays of Object. Extension methods require the parameters to be inferred from the collection type and then the predicate checked to be of the correct type.

此外,对于要应用的扩展方法,必须强制执行类型化集合,这在PowerShell中很难实现。 PowerShell作为一种动态语言,支持构建各种类型的集合,大多数集合都表示为Object数组。扩展方法要求从集合类型推断出参数,然后检查谓词的类型是否正确。

If your concern is for some LINQ like functions, there are a number of cmdlets that provide the same functionality in working with object collections.

如果您关注某些类似LINQ的函数,则有许多cmdlet在处理对象集合时提供相同的功能。

PowerShell's extended type system allows you to add methods to various types by adding an xml file or modifying an existing one (creating a new one is the recommended path). Jeffrey Snover demonstrates doing this with adding a ScriptProperty to the Object class in this blog post.

PowerShell的扩展类型系统允许您通过添加xml文件或修改现有文件来向各种类型添加方法(建议使用新文件作为推荐路径)。 Jeffrey Snover通过在此博客文章中向Object类添加ScriptProperty来演示此操作。

It's not quite the same, but it could get the job done.

它不太一样,但它可以完成工作。

#1


It doesn't get easier in V2, but there is an extension mechanism which you might not be aware of.

它在V2中并不容易,但有一种你可能不知道的扩展机制。

I believe that part of the problem is in PowerShell's handling (or lack thereof) of generics.

我认为问题的一部分在于PowerShell处理(或缺乏)泛型。

Also, for extension methods to be applied, the typed collections would have to be enforced, which is difficult in PowerShell. PowerShell, as a dynamic language, supports building collections of various types and most collections are represented as arrays of Object. Extension methods require the parameters to be inferred from the collection type and then the predicate checked to be of the correct type.

此外,对于要应用的扩展方法,必须强制执行类型化集合,这在PowerShell中很难实现。 PowerShell作为一种动态语言,支持构建各种类型的集合,大多数集合都表示为Object数组。扩展方法要求从集合类型推断出参数,然后检查谓词的类型是否正确。

If your concern is for some LINQ like functions, there are a number of cmdlets that provide the same functionality in working with object collections.

如果您关注某些类似LINQ的函数,则有许多cmdlet在处理对象集合时提供相同的功能。

PowerShell's extended type system allows you to add methods to various types by adding an xml file or modifying an existing one (creating a new one is the recommended path). Jeffrey Snover demonstrates doing this with adding a ScriptProperty to the Object class in this blog post.

PowerShell的扩展类型系统允许您通过添加xml文件或修改现有文件来向各种类型添加方法(建议使用新文件作为推荐路径)。 Jeffrey Snover通过在此博客文章中向Object类添加ScriptProperty来演示此操作。

It's not quite the same, but it could get the job done.

它不太一样,但它可以完成工作。