title: Using to get PS script name from a C# DLL? Getting "Object reference not set to an instance of an object."
body: <p>ERROR:</p>
  <blockquote>
  <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  <p>I'm trying to create a DLL that can get the Entrypoint application name that is written in Powershell.</p>
  <p>The layout is very simple:<br />
  In Powershell I add the line</p>
  <pre><code>Add-Type -Path ('C:\Users\Public\PS\DLL\get_value.dll')
  </code></pre>
  <p>then I create the object</p>
  <pre><code>$value = New-Object PS_get_value.Stack
  </code></pre>
  <p>and call the C# method</p>
  <pre><code>$rtn = $value.Get_stack()
  </code></pre>
  <p>If the <code>Get_stack()</code> method returns a basic string like <code>&quot;Test&quot;</code> it works fine….
  BUT if I try to use the .net Assembly.<code>GetEntryAssembly().Location</code> (which should return the full path to the calling (parent) app), I get the error above.</p>
  <p>I am trying to get the full path (or at least the filename) of the application script that is running in Powershell. I plan on using that value to validate that the program (PS script) is the one that should be running. If there is a different way for the C# code to get it that's fine. BTW if I run a normal program it works. I do know that there is something about managed code base, I'm not sure what that is.<br />
  Thanks for any help or direction your can give.</p>
  <p>Code Example:<br />
  PS:</p>
  <pre><code>Add-Type -Path ('C:\Users\Public\PS\DLL\get_value.dll')
  $addNum = New-Object PS_get_value.Stack
  $_rtnKey = $addNum.Get_stack()
  Write-Host &quot;RTNed Key Version 2: $_rtnKey&quot;
  </code></pre>
  <p>C# (the DLL):</p>
  <pre><code>using System;
  using System.IO;
  using System.Diagnostics;
  using System.Reflection;
  
  namespace PS_get_value
  {
      public class Stack{
        public string Get_stack()
          {
              string _test_a = g_stack();
              return _test_a;
          }
  
        public string g_stack()
          {
              return System.Reflection.Assembly.GetEntryAssembly().Location;
          }
     }
  }
  </code></pre>