How do I add an event handler to a .NET object in PowerShell?

Question:

I’m writing a PowerShell script which uses a third-party library. One of the objects I’m using requires that I add an event handler. The event delegate looks like this:

In PowerShell, I tried doing this:

But I get back this error message:

Is there a better way of subscribing to events? Why can’t the Stream type be used as a type argument? What am I doing wrong?

Answer:

You should use the Register-ObjectEvent cmdlet, take a look at the examples on your own system by using:

Source:

How do I add an event handler to a .NET object in PowerShell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply