HighTechTalks DotNet Forums  

Code working in 1.1 doesnt work in 2.0

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Code working in 1.1 doesnt work in 2.0 in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Piyush Daiya
 
Posts: n/a

Default Code working in 1.1 doesnt work in 2.0 - 05-03-2007 , 03:38 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.CheckConsisten cy(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(Obj ect 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



Reply With Quote
  #2  
Old   
Piyush Daiya
 
Posts: n/a

Default Re: Code working in 1.1 doesnt work in 2.0 - 05-04-2007 , 01:21 PM






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 (AT) primeassociates (DOT) com> wrote

Quote:
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.CheckConsisten cy(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(Obj ect 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





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.