Link to home
Start Free TrialLog in
Avatar of cmhunty
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 PropertyDescriptorCollection = TypeDescriptor.GetProperties(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.GetProperties(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.
ASKER CERTIFIED SOLUTION
Avatar of jake072
jake072
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cmhunty
cmhunty

ASKER

I put together a test script just before leaving work yesterday and it didn't unfortunately. If it's ok, I'll post on here when I get back to work today so I can confirm it's not my system setup that's causing the problem 'cos this seems really strange. I've checked the books online and there doesn't seem to be anything in version 2.0 that suggests that these methods have been reworked.

Cheers for your help.

Chris
Avatar of cmhunty

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_TypeDescriptor(test))
        Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflection(test, "name"))
        'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflection(test, "TestProp1"))
        'Console.WriteLine("Web service class, reflection - exists: " & DoesPropertyExist_Reflection(test, "rows"))
        Console.Read()
    End Sub

    Public Function GetPropertyCount_TypeDescriptor(ByVal obj As Object) As Int32
        Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(obj)
        Return props.Count
    End Function

    Public Function DoesPropertyExist_Reflection(ByVal obj As Object, ByVal strPropertyName As String) As Boolean

        Dim blFound As Boolean = False
        Dim info As PropertyInfo = obj.GetType().GetProperty(strPropertyName)

        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
Avatar of cmhunty

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,

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
Avatar of cmhunty

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
My Extended Browser:

http://www.nymtec.com/?q=node/97

Jake
Avatar of cmhunty

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!