[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Code working in 1.1 doesnt work in 2.0

Piyush Daiya

5/3/2007 7:39:00 PM

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


1 Answer

Piyush Daiya

5/4/2007 5:21:00 PM

0

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

"Piyush Daiya" <pdaiya@primeassociates.com> wrote in message
news:u45ORrbjHHA.4552@TK2MSFTNGP04.phx.gbl...
> 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
>
>