cmhunty
asked on
Problem downgrading TypeDescriptor.GetProperties from .NET 2.0 to 1.1
Hi
I have this function in VB.NET 2.0 and it works fine.
Public Function GetObjectProperty(ByVal obj As Object, ByVal strPropertyName As String) As Object
Dim props As PropertyDescriptorCollecti on = TypeDescriptor.GetProperti es(obj)
Dim objtype As String = obj.GetType.ToString
Dim blFoundProperty As Boolean = False
Dim objOut As New Object
' Iterate properties, try and find the one we're looking for. If found, set it.
For Each prop As PropertyDescriptor In props
If prop.Name = strPropertyName Then
objOut = prop.GetValue(obj)
blFoundProperty = True
Exit For
End If
Next
' If the property hasn't been found, throw an exception
If blFoundProperty = False Then
Throw New Exception("Property " & strPropertyName & " not found in object type " & objType.ToString)
End If
Return objOut
End Function
The function simply sets a specific property of an object to value.
I've had to downgrade the class to .NET 1.1 and I've hit problems when I supply a custom object. TypeDescriptor.GetProperti es(obj) returns no properties. All still works fine with System objects.
Does anyone have any thoughts on this?
If not, can anyone think of another way to return a property of an object when the property name is held in a string?
Thanks in advance for any help.
I have this function in VB.NET 2.0 and it works fine.
Public Function GetObjectProperty(ByVal obj As Object, ByVal strPropertyName As String) As Object
Dim props As PropertyDescriptorCollecti
Dim objtype As String = obj.GetType.ToString
Dim blFoundProperty As Boolean = False
Dim objOut As New Object
' Iterate properties, try and find the one we're looking for. If found, set it.
For Each prop As PropertyDescriptor In props
If prop.Name = strPropertyName Then
objOut = prop.GetValue(obj)
blFoundProperty = True
Exit For
End If
Next
' If the property hasn't been found, throw an exception
If blFoundProperty = False Then
Throw New Exception("Property " & strPropertyName & " not found in object type " & objType.ToString)
End If
Return objOut
End Function
The function simply sets a specific property of an object to value.
I've had to downgrade the class to .NET 1.1 and I've hit problems when I supply a custom object. TypeDescriptor.GetProperti
Does anyone have any thoughts on this?
If not, can anyone think of another way to return a property of an object when the property name is held in a string?
Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gets stranger and stranger. The problem only seems to be with classes from a web reference. Put together the code below.
User defined classes work fine in both 1.1 and 2.0.
Classes from separate dll assembly work fine in both 1.1 and 2.0.
Web referenced classes only work in 2.0.
As mentioned there's nothing in MS documentation that ComponentModel or Reflection shouldn't work in 1.1 so I'm going to take this up with them.
Thank you again for your help.
Chris
-------------------------- ---------- ---------- ---------- ------
Imports System.ComponentModel
Imports System.Reflection
Imports Test2.cmrws
Module Module1
Sub Main()
Dim test As New Customer
'Dim test As New TestClass
'Dim test As New Data.DataTable
Console.WriteLine("Web service class, type descriptor count: " & GetPropertyCount_TypeDescr iptor(test ))
Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti on(test, "name"))
'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti on(test, "TestProp1"))
'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti on(test, "rows"))
Console.Read()
End Sub
Public Function GetPropertyCount_TypeDescr iptor(ByVa l obj As Object) As Int32
Dim props As PropertyDescriptorCollecti on = TypeDescriptor.GetProperti es(obj)
Return props.Count
End Function
Public Function DoesPropertyExist_Reflecti on(ByVal obj As Object, ByVal strPropertyName As String) As Boolean
Dim blFound As Boolean = False
Dim info As PropertyInfo = obj.GetType().GetProperty( strPropert yName)
If info Is Nothing = False Then
blFound = True
End If
Return blFound
End Function
Public Class TestClass
Private strProp1 As String
Private strProp2 As String
Public Property TestProp1()
Get
Return strProp1
End Get
Set(ByVal value)
strProp1 = value
End Set
End Property
Public Property TestProp2()
Get
Return strProp2
End Get
Set(ByVal value)
strProp2 = value
End Set
End Property
End Class
End Module
User defined classes work fine in both 1.1 and 2.0.
Classes from separate dll assembly work fine in both 1.1 and 2.0.
Web referenced classes only work in 2.0.
As mentioned there's nothing in MS documentation that ComponentModel or Reflection shouldn't work in 1.1 so I'm going to take this up with them.
Thank you again for your help.
Chris
--------------------------
Imports System.ComponentModel
Imports System.Reflection
Imports Test2.cmrws
Module Module1
Sub Main()
Dim test As New Customer
'Dim test As New TestClass
'Dim test As New Data.DataTable
Console.WriteLine("Web service class, type descriptor count: " & GetPropertyCount_TypeDescr
Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti
'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti
'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflecti
Console.Read()
End Sub
Public Function GetPropertyCount_TypeDescr
Dim props As PropertyDescriptorCollecti
Return props.Count
End Function
Public Function DoesPropertyExist_Reflecti
Dim blFound As Boolean = False
Dim info As PropertyInfo = obj.GetType().GetProperty(
If info Is Nothing = False Then
blFound = True
End If
Return blFound
End Function
Public Class TestClass
Private strProp1 As String
Private strProp2 As String
Public Property TestProp1()
Get
Return strProp1
End Get
Set(ByVal value)
strProp1 = value
End Set
End Property
Public Property TestProp2()
Get
Return strProp2
End Get
Set(ByVal value)
strProp2 = value
End Set
End Property
End Class
End Module
ASKER
Have run on another machine and can confirm that this isn't an environment issue. Have raised an issue with MS. Will post their response. Thanks for your help.
Chris
Chris
Chris,
When you say this is only for web interfaces, I may have some insight...
Personally, I have a .NET 1.1 project that I've been working on for the past 3 years... Once MS came out with IE 7, I noticed that all my web browsers that are embedded in my project stopped working properly.
I have a custom inherited browser that I've made, I'm just trying to figure out how to post it somewhere to share with others... Anyways, the long and the short is that I've found that IE 7 completely broke most AxSchdocvw browsers; perhaps that has something to do with it?
I'd love to hear MS's response...
P.S. Where do you contact MS regarding these types of issues? Do you need an MSDN?
Thanks,
Jake
When you say this is only for web interfaces, I may have some insight...
Personally, I have a .NET 1.1 project that I've been working on for the past 3 years... Once MS came out with IE 7, I noticed that all my web browsers that are embedded in my project stopped working properly.
I have a custom inherited browser that I've made, I'm just trying to figure out how to post it somewhere to share with others... Anyways, the long and the short is that I've found that IE 7 completely broke most AxSchdocvw browsers; perhaps that has something to do with it?
I'd love to hear MS's response...
P.S. Where do you contact MS regarding these types of issues? Do you need an MSDN?
Thanks,
Jake
ASKER
Cheers for that! I'll have a little bit of a further look.
Our company is a MS Partner so we have a support account with them. Still waiting to hear from them. Turnaround is usually 1-2 days. Will keep you posted.
Cheers
Chris
Our company is a MS Partner so we have a support account with them. Still waiting to hear from them. Turnaround is usually 1-2 days. Will keep you posted.
Cheers
Chris
ASKER
Jake - fyi, I raised the issue with Microsoft and eventually got the answer back. The wsdl.exe on 1.1 differs from 2.0. 1.1 treats properties as public fields and therefore none of the above solutions work. I had a reasonable arguement with MS when they charged my for the support as my arguement was that it wasn't documented anywhere. They dug out this article: http://msdn.microsoft.com/msdnmag/issues/05/04/NETMatters/
Just thought you may be interested!
Just thought you may be interested!
ASKER
Cheers for your help.
Chris