Code working in 1.1 stops working in 2.0

P

Piyush Daiya

Hi All,

I have this following code which works fine in 1.1 but gives runtime
exception 2.0:-


Reflection Code to get One Property from COM Object

-------------------------------------------------------------------------------



Dim objSession as Interop.ComObj.Session

Dim objAgent as Interop.ComObj.Agent

Dim objAgentType as Type

Dim PropertyName as String

Dim itemProperty as PropertyInfo

Dim propertyValue as Object



objAgent = objSession.Create("Agent")

objAgentType = GetType(Interop.ComObj.AgentClass) ' Important to use
Class and not Interface. If you use Interface the reflection does not find
the property.



itemProperty = objAgentType.GetProperty(PropertyName) ' make sure the
Propertyname exactly matches the property of COM object( including case)



'Get Property Value



propertyValue = itemProperty.GetValue(objAgent, Nothing)

If Not (propertyValue Is Nothing) Then

ItemValue = propertyValue.ToString()



Else

ItemValue = String.Empty



End If





'Set Property Value



Dim NewValue as String = "TestValue"



itemProperty.SetValue(objCustomer, System.Convert.ChangeType(NewValue,
itemProperty.PropertyType), Nothing) ' This line gives error. The COM
Property is String type





The error is given below:-





Error Description:

Object does not match target type.

Source:

mscorlib

Stack Trace:

at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
skipVisibilityChecks)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value,
BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)

at CMBrowser.OnBoardCustomer.SetPropertyValueEx(Type& objType, String
ColumnName, String ItemValue, Object& objCom) in
C:\SRCREF\CMBrowser\OnBoardCustomer.aspx.vb:line 4314

Class: RuntimeMethodInfo

Proc Name: CheckConsistency



Any clues as to why this is happening?

This exception occurs for all properties irrespective of their types.







Piyush
 
P

Piyush Daiya

Found the solution,

just replace



the line



objAgentType = GetType(Interop.ComObj.AgentClass) ' Important to use
Class and not Interface. If you use Interface the reflection does not find
the propert



with





objAgentType = GetType(Interop.ComObj._Agent)





Apparently, _PropertyName is new in 2.0.





Piyush
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top