PC Review


Reply
Thread Tools Rate Thread

Code working in 1.1 stops working in 2.0

 
 
Piyush Daiya
Guest
Posts: n/a
 
      3rd May 2007
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


 
Reply With Quote
 
 
 
 
Piyush Daiya
Guest
Posts: n/a
 
      4th May 2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form code stops working davea Microsoft Access Forms 1 21st Oct 2010 04:45 AM
Code runs for a while, then stops working Andrew Microsoft Excel Programming 2 18th Oct 2007 02:36 PM
VB Code stops working =?Utf-8?B?TWljaGVsbGUgSw==?= Microsoft Excel Programming 17 17th Aug 2007 05:58 PM
Code working in 1.1 stops working in 2.0 Piyush Daiya Microsoft Dot NET 1 4th May 2007 06:21 PM
dim statement stops code from working at all! Perry Microsoft Access VBA Modules 3 13th Apr 2004 10:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:29 AM.