Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easier way of using extension methods in Powershell v2

Background

this post explains how one can consume extension methods in 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.

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.

like image 384
namenlos Avatar asked Jan 10 '09 20:01

namenlos


People also ask

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

Should you use extension methods?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

What is used of extension methods and how do you create and use it?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

How are extension methods implemented?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.


1 Answers

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

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

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.

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.

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.

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

like image 175
Steven Murawski Avatar answered Nov 15 '22 08:11

Steven Murawski