title: How do I add an event handler to a .NET object in PowerShell?
body: <p>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.  <a href="http://www.eldos.com/documentation/sbb/documentation/ref_cl_pgpreader_evt_oncreateoutputstream.html" rel="noreferrer">The event delegate looks like this</a>:</p>
  
  <pre><code>public delegate void TSBPGPCreateOutputStreamEvent(object Sender, 
                                                     string Filename, 
                                                     System.DateTime TimeStamp, 
                                                     ref System.IO.Stream Stream, 
                                                     ref bool FreeOnExit)
  </code></pre>
  
  <p>In PowerShell, I tried doing this:</p>
  
  <pre><code>$reader.Add_OnCreateOutputStream( {
      param(
          $sender,
          $fileName,
          $timestamp,
          [ref]
          $stream,
          [ref]
          $freeOnExit
      )
      $stream = New-Object IO.MemoryStream
  } )
  </code></pre>
  
  <p>But I get back this error message:</p>
  
  <pre><code> Cannot convert value "
      param(
          $sender,
          $fileName,
          $timestamp,
          [ref]
          $stream,
          [ref]
          $freeOnExit
      )
  $stream = New-Object IO.MemoryStream
  " to type "SBPGP.TSBPGPCreateOutputStreamEvent". 
  Error: "The type 'System.IO.Stream&amp;' may not be used as a type argument."
  </code></pre>
  
  <p>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?</p>