title: Using custom type in Powershell 5.0 class
body: <p>Here is example code that is causing me lots of headaches at the moment.</p>
  
  <pre class="lang-pshell prettyprint-override"><code>if (("Win32.NativeMethods" -as [type]) -eq $null){
      Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
          ' -name NativeMethods -namespace Win32
  }
  
  class AppInstance 
  {    
      [string]$App = 'Notepad.exe'
      [IntPtr]$hWnd = 0
      [System.Object]$process
  
      AppInstance () {
          Start-Process $this.App
          $this.process = get-process ($this.App.split('.'))[0]
          start-sleep -Milliseconds 100
          $this.hWnd = $this.process.MainWindowHandle    
      }
  
      [void] Show () {
          [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 3)
      }
  
      [void] Hide () {
          [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 2)
      }
  }
  </code></pre>
  
  <p>This class can be used like so</p>
  
  <pre class="lang-pshell prettyprint-override"><code>$notepad = [AppInstance]::new()
  $notepad.Hide()
  $notepad.Show()
  </code></pre>
  
  <p>Basically, what I'm trying to do is to import a function from <code>user32.dll</code> as type <code>[Win32.NativeMethods]</code> and then use this type in a <code>class</code>.</p>
  
  <p>If I execute the <code>Add-Type</code> statement separately in <code>Powershell_ISE</code> the type gets created and subsequently the script works just fine.</p>
  
  <p>However, when I try to execute the whole script before creating the type manually, I get the following Powershell parser error</p>
  
  <pre><code>At C:\class.ps1:26 char:10
  +         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 3)
  +          ~~~~~~~~~~~~~~~~~~~
  Unable to find type [Win32.NativeMethods].
  At C:\Uclass.ps1:31 char:10
  +         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 2)
  +          ~~~~~~~~~~~~~~~~~~~
  Unable to find type [Win32.NativeMethods].
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : TypeNotFound
  </code></pre>
  
  <p>Looks like the parser is ignoring the <code>Add-Type</code> statement and exiting before execution. </p>
  
  <p>Is there any way to overcome this issue? Maybe with a <code>using</code> statement?
  Or, is there any way to tell the parser that the type is dynamically created? </p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I have read the answer to <a href="https://stackoverflow.com/questions/34625440/using-net-objects-within-a-powershell-v5-class">Using .Net Objects within a Powershell (V5) Class</a> and the accepted answer is not an answer to my question. Splitting a simple script into multiple files is not really an answer.</p>
  
  <p>What I'm asking is weather there is a way to tell the parser that the type is dynamically created. </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>To clarify this a little further here is code equivalent to the one above, but implemented using <code>functions</code> instead of <code>classes</code>.</p>
  
  <pre class="lang-psell prettyprint-override"><code>if (("Win32.NativeMethods" -as [type]) -eq $null){
      Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
          ' -name NativeMethods -namespace Win32
  }
  
  [string]$script:App = 'Notepad.exe'
  
  $process = Start-Process $App -PassThru
  
  
  function Show () {
      [Win32.NativeMethods]::ShowWindowAsync($process.MainWindowHandle, 3)
  }
  
  function Hide () {
      [Win32.NativeMethods]::ShowWindowAsync($process.MainWindowHandle, 2)
  }
  </code></pre>
  
  <p>This code will parse and execute perfectly fine. Are <code>classes</code> handled by the parser in a different way to rest of the script?</p>