views:

32

answers:

2

I have been playing around with the dynamic abilities of powershell and I was wondering something

Is there is anything in powershell analogous to Ruby's method_missing() where you can set up a 'catch all method' to dynamically handle calls to non-existant methods on your objects?

A: 

You could use Try Catch within Powershell 2.0

http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx

cbattlegear
This would allow me to handle the case but from the wrong place. I want the object itself to respond usefully to calls to a method that doesn't exist
Willbill
+1  A: 

No, not really. I suspect that the next version of PowerShell will become more in line with the dynamic dispatch capabilities added to .NET 4 but for the time being, this would not be possible in pure PowerShell.

Although I do recall that there is a component model similar to that found in .NET's TypeDescriptor for creating objects that provide properties and methods dynamically to PowerShell. This is how XML elements are able to be treated like objects, for example. But this is poorly documented if at all and in my experience, a lot of the types/methods needed to integrate are marked as internal.

Josh Einstein
+1 It was the [xml] literal that made me think this must be possible
Willbill