title: How to call a COM Object by ProgID with Reflection in C#
body: <pre><code>class Device{
      private object device;
      public Device(string ProgID)
      {
          if (ProgID == "") ProgID = "ScopeSim.Telescope";
          device = Activator.CreateInstance(Type.GetTypeFromProgID(ProgID));
          Console.WriteLine("Connected");
      }
      public object Invoke(string Name, object[] args)
      {
          var v1 = device.GetType(); //this is a com object in debug
          var v2 = v1.GetMethod(Name);
          var v3 = v2.Invoke(device,args); //throws exception, v2 is null
          return v3;
      }
  }
  //somwhere else in another method in another class that has this in a field...
  Console.WriteLine(new Device("").Invoke("A Method Name that is a string but is not known and could be anything, for testing, the name is 'Unpark'", object[] args));
  </code></pre>
  
  <p>This throws a <code>NullReferenceException</code>. The Unpark method does exist but it does not have a return type, but it does exist. Also, when it stopped to debug (on the exception) the ProgID field in the constructor was null. I would assume that this is normal though, right? It would have already run. Does anyone know why it throws it? If I declare device as <code>dynamic</code>, says it can't bind at runtime to a null object (basically the same thing).</p>
  
  <p><strong>Response to First Answer:</strong> I think reflection requires the variables as an array of objects. Yes, Unpark is written with a capital U. The ProgID thing apparently seems to be irrelevant.</p>