Is there an easier way to use extension methods in Powershell v2

Background

This post explains how to use extension methods in Powershell

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

Compare this with what someone is doing in C # - they add "using SomeAssembly" and all extension methods are loaded.

My questions

It has become easier than Powershell 2.0. And if so, what should I do to use extension methods in Powershell 2.0? I checked the public documentation and installed CTP and I see nothing that helps.

+6
powershell extension-methods
source share
1 answer

In V2, this does not get easier, but there is an extension mechanism that you may not be aware of.

I believe that part of the problem is related to the processing of PowerShell (or lack of) generic data.

In addition, typed collections must be introduced to use extension methods, which is difficult in PowerShell. PowerShell, as a dynamic language, supports the collection of collections of various types, and most collections are represented as arrays of Object. Extension methods require that the parameters be inferred from the type of the collection, and then it is verified that the predicate is of the correct type.

If your problem is related to some functions like LINQ, there are several cmdlets that provide the same functionality when working with collections of objects.

The extended PowerShell 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 way). Jeffrey Sverver demonstrates this by adding ScriptProperty to the Object class in this blog post .

This is not exactly the same, but it can do the job.

+3
source share

All Articles