Reputation: 7216
I'm trying to write a binary module for PowerShell. However I have a problem as I'd like to export common functionality into a helper method:
class Foo {
Bar DoBaz() {
if (bazzed) {
WriteWarning(this.ToString() + " already bazzed");
return baz;
}
// ...
}
}
This of course doesn't work as WriteVerbose
is a method of Cmdlet
. I can pass it as lambda but this seems to be very roundabout way of doing it.
Upvotes: 2
Views: 174
Reputation: 4173
You have to pass the Cmdlet
(or more commonly PSCmdlet
) instance to the helper method. Here's an example
using System.Management.Automation;
[Cmdlet(VerbsDiagnostic.Test, "Cmdlet")]
public class TestCmdletCommand : PSCmdlet
{
protected override void ProcessRecord()
{
HelperMethods.WriteFromHelper(this, "message");
}
}
public static class HelperMethods
{
public static void WriteFromHelper(PSCmdlet cmdlet, string message)
{
cmdlet.WriteVerbose(message);
}
}
Upvotes: 3