title: Invoke method dynamically in VB.Net
body: <p>I've some classes defined in a dll file. These are in the form of com api.</p>
  <p>I'm trying to create an object of one of the class dynamically and than setting some property of that object.</p>
  <p>When I set the property manually, it works, but when I try to invoke the same using reflection it gives the error that</p>
  <p>Object does not match target type.</p>
  <p>Following is my code</p>
  <pre><code>Private Sub SetObjectValue(ByVal SelectedObject As SAPbobsCOM.BoObjectTypes, ByVal ClassName As String, ByVal FieldName As String, ByVal SetValue As String, ByVal KeyValue As String)
      Dim oObject As Object
  
      Dim myAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(&quot;interop.sapbobscom.dll&quot;)
      Dim myType As Type = myAssembly.GetType(ClassName)
  
      Dim myMember() As MemberInfo = myType.GetMember(FieldName)
      Dim myProperty As PropertyInfo = CType(myMember(0), PropertyInfo)
      Dim myMethod As MethodInfo = myProperty.GetSetMethod
  
   
     oObject = oCompany.GetBusinessObject(SelectedObject)
  
      oObject.GetByKey(KeyValue)
  
      myProperty.SetValue(oObject, CDbl(SetValue), Nothing)
  End Sub
  </code></pre>
  <p>It gives the error when SetValue method is called. Instead, if I use this like following it works fine:</p>
  <pre><code>oObject.CreditLimit = 129
  oObject.Update
  </code></pre>
  <p>Where CreditLimit is a property of the given class, and update is a method which I have to call, after the value is set, so that the value in underlying database is updated.</p>
  <p>Similarly GetByKey is used to retrieve the value of the object from the underlying database, where the value of the primary key field has to be passed.</p>
  <p>Since there are multiple classes and each class has lots of different properties, therefore calling them dynamically will help a lot.</p>
  <p>Thanks
  Rahul Jain</p>
  <p>Just tried doing what casper has suggested here. It gives an error saying - Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))</p>
  <p>Rahul</p>
  <blockquote>
  <p>Its done. Instead of vbSet, I used vbLet and it completed successfully.</p>
  <p>Thanks
  Rahul</p>
  </blockquote>