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
' 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)
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.