title: Invoking COM Properties and Methods
body: <p>I am trying to dynamically create COM object, call COM method and set COM properties. The COM class is a VB6 ActiveX DLL. The implementation is exactly equal to the VB6 code from this page <a href="http://msdn.microsoft.com/en-us/library/ms973800.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms973800.aspx</a>.</p>
  
  <p>In short words, the project is <code>PhysServer</code> and the class name is <code>Temperature</code> which has two properties <code>Celsius</code> and <code>Fahrenheit</code> and two methods <code>GetCelsius()</code> and <code>GetFahrenheit()</code>.</p>
  
  <p>I have already run <code>regsvr32</code> to register the ActiveX DLL to the computer. The ProgID is <code>PhysServer.Temperature</code>.</p>
  
  <p>I have three block of codes</p>
  
  <p><strong>Code Block 1 (works)</strong></p>
  
  <pre><code>Option Explicit Off
  Option Strict Off
  ...
  Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
  Dim comObj = Activator.CreateInstance(objType)
  comObj.Celsius = 100
  Dim f As Double = comObj.GetFahrenheit()
  Console.WriteLine(f) ' shows 212
  </code></pre>
  
  <p><strong>Code Block 2 (works)</strong></p>
  
  <pre><code>Option Explicit On
  Option Strict On
  ...
  Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
  Dim comObj = Activator.CreateInstance(objType)
  Microsoft.VisualBasic.CallByName(comObj, "Celsius", CallType.Let, 100)
  Dim f As Double = CDbl(Microsoft.VisualBasic.CallByName(comObj, "GetFahrenheit", CallType.Method, Nothing))
  Console.WriteLine(f) ' shows 212
  </code></pre>
  
  <p><strong>Code Block 3 (doesn't work)</strong></p>
  
  <pre><code>Option Explicit On
  Option Strict On
  ...
  Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
  Dim comObj = Activator.CreateInstance(objType)
  Dim f As Double = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
  Console.WriteLine(f) ' shows the default value of GetFahrenheit '
  objType.InvokeMember("Celsius", Reflection.BindingFlags.SetField Or Reflection.BindingFlags.InvokeMethod, Nothing, comObj, New Object() {100}) ' throws exception: Number of parameters specified does not match the expected number '
  f = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
  Console.WriteLine(f)
  </code></pre>
  
  <p>I understand Code Block 1 and Code Block 2. However, how could I use set a COM object using reflection like Code Block 3? By some reasons, I cannot use Code Block 1 and Code Block 2. So the only way is Code Block 3... but it doesn't work.</p>
  
  <p>Does anyone know the solution of Code Block 3? Thanks!</p>