title: Specified Cast is Invalid Getting MAPIOBJECT from MailItem.AddressEntry using Late Binding
body: <p>I'm trying to get the MAPIOBJECT of the MailItem.AddressEntry using Late Binding with Outlook. </p>
  
  <p>I keep getting "Exception has been thrown by target of invocation" and an inner exception of "Specified Cast is Invalid" and I have no idea why. Nothing has come up on Google searches etc.</p>
  
  <p>Firstly I know that the MAPIOBJECT is deprecated and not visible through intellisense but works.</p>
  
  <p>I can get the object no problems without late binding.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Gets the MAPI Object from the AddressEntry of the new recipient.
  /// &lt;/summary&gt;
  /// &lt;param name="senderName"&gt;The name of the sender to add to the recipients.&lt;/param&gt;
  /// &lt;param name="outlookApplication"&gt;The Outlook Application instance.&lt;/param&gt;
  private static object GetMapiObject(string senderName, object outlookApplication)
  {
      var mailItem = InvokeMember("CreateItem", outlookApplication, new object[] { 0 });
      var recipients = GetProperty("Recipients", mailItem);
      var recipient = InvokeMember("Add", recipients, new object[] { senderName });
  
      InvokeMember("Resolve", recipient, new object[] {});
      var addressEntry = GetProperty("AddressEntry", recipient);
  
      var mapiObject = GetProperty("MAPIOBJECT", addressEntry); // Error occurs here.
      return mapiObject;
  }
  
  /// &lt;summary&gt;
  /// Gets a property of an instance by its name
  /// &lt;/summary&gt;
  /// &lt;param name="propertyName"&gt;The property name to get.&lt;/param&gt;
  /// &lt;param name="instance"&gt;The object to get the property from.&lt;/param&gt;
  /// &lt;returns&gt;The resulting object.&lt;/returns&gt;
  private static object GetProperty(string propertyName, object instance)
  {
      Type type = instance.GetType();
      return type.InvokeMember(propertyName, BindingFlags.GetProperty, null, instance, new object[] { });
  }
  
  /// &lt;summary&gt;
  /// Invoke an object by its method and type - takes parameters.
  /// &lt;/summary&gt;
  /// &lt;param name="method"&gt;The method to invoke.&lt;/param&gt;
  /// &lt;param name="instance"&gt;The object that contains the method to invoke.&lt;/param&gt;
  /// &lt;param name="parameters"&gt;The parameters to pass to the method.&lt;/param&gt;
  /// &lt;returns&gt;The resulting object.&lt;/returns&gt;
  private static object InvokeMember(string method, object instance, object[] parameters)
  {
      try
      {
          Type type = instance.GetType();
          return type.InvokeMember(method, BindingFlags.InvokeMethod, null, instance, parameters);
      }
      catch (Exception ex)
      {
          switch (method)
          {
             case "SaveAsFile":
                  throw new System.IO.IOException("Error occurred during \"SaveAsFile\" for attachments. Attachment filename may be too long. ", ex);                                                
  
              default:
                  throw new TargetInvocationException("Handled error at Invoke Member. Method Name: " + method, ex);
          }
      }
  }
  </code></pre>