Introduction
Sometimes, it is useful to know if an object has a certain property and then fill it by only having the propertyname
as a string
at hand.
Background
The core can be found in 2 functions. They are a bit FAT but I copied them from a project I made where I first generate VB.NET code for an Class entity using the data returned from a .NET DataTable
object. Next, this entity can bind itself as long as the fieldnames don't change in the database.
I included a sample project where we were experimenting a bit with this. You are welcome to use it.
The Code
The 2 functions are given below:
GET_SET_PropertyValue(name as string , optional Value as object)
findproperty(ByVal name As String, ByRef myProps As PropertyInfo) As String
Function 2 iterates through all properties and looks for the one you need. Function one sets and gets a property by only querying the string
you enter at first. If it does not find the property, it uses Findproperty
to look a bit deeper.
Private Function GET_SET_PropertyValue_
(name As String, Optional value As Object = Nothing) As List(Of Object)
Dim OriginalName As String = ""
Dim propertyType As System.Type = Nothing
Dim myProps As System.Reflection.PropertyInfo = Nothing
Dim PropValue As Object = Nothing
Dim returnlist As New List(Of Object)
Dim SetterValueType As System.Type = Nothing
Try
myProps = Me.GetType.GetProperty(name)
If Not (myProps Is Nothing) Then
OriginalName = name
Else
OriginalName = findproperty(name, myProps)
End If
If myProps Is Nothing Or OriginalName = "" Then
returnlist.Add("error")
returnlist.Add(name)
returnlist.Add(name & " as a property name was not found.")
returnlist.Add(name.GetType)
Return returnlist End If
Catch ex As Exception
If returnlist.Count > 0 Then returnlist = New List(Of Object)
returnlist.Add("error")
returnlist.Add(name)
returnlist.Add(ex.Message)
returnlist.Add(Err.GetType.ToString)
Return returnlist End Try
If (value Is Nothing) Then Try propertyType = myProps.GetType
PropValue = myProps.GetValue(Me, Nothing)
returnlist.Add("success")
returnlist.Add(OriginalName)
returnlist.Add(PropValue)
returnlist.Add(propertyType.GetType.ToString)
Return returnlist Catch ex As Exception
If returnlist.Count > 0 Then returnlist = New List(Of Object)
returnlist.Add("error")
returnlist.Add(name)
returnlist.Add(ex.Message)
returnlist.Add(Err.GetType.ToString)
Return returnlist End Try
End If
Try propertyType = myProps.GetType
SetterValueType = value.GetType
myProps.SetValue(Me, value)
returnlist.Add("success")
returnlist.Add(OriginalName)
returnlist.Add("your object type: " & SetterValueType.GetType.ToString)
returnlist.Add("the property type:" & propertyType.GetType.ToString)
Return returnlist
Catch ex As Exception
returnlist.Add("error")
returnlist.Add(OriginalName)
returnlist.Add(name)
returnlist.Add(ex.Message)
returnlist.Add(Err.GetType.ToString)
returnlist.Add("your object type: " & SetterValueType.GetType.ToString)
returnlist.Add("the property type:" & propertyType.GetType.ToString)
Return returnlist End Try
End Function
Private Function findproperty(ByVal name As String, ByRef myProps As PropertyInfo) As String
Dim myMembers As PropertyInfo() = Me.GetType.GetProperties()
Dim propertyname As String = ""
Try
If myMembers.Length > 0 Then
Dim index As Integer
For index = 0 To myMembers.Length - 1
If LCase(myMembers(index).Name) = (LCase(name)) Then
myProps = myMembers(index)
propertyname = myMembers(index).Name
Return propertyname
End If
Next index
myProps = Nothing
Return propertyname
Else
myProps = Nothing
Return propertyname
End If
Catch ex As Exception
myProps = Nothing
Return propertyname
End Try
End Function
Getting the Properties from an Object
It is useful to use this one first and then set or read the properties with the code above.
Public Function getAllProperties(Of T)(foreigner As T) As String()
Dim pString As String()
Try
Dim myMembers As PropertyInfo() = foreigner.GetType.GetProperties()
If myMembers.Length > 0 Then
pString = New String(myMembers.Length - 1) {}
Dim index As Integer
For index = 0 To myMembers.Length - 1
pString(index) = myMembers(index).Name
Next index
Return pString
Else
pString = New String(0) {}
pString(0) = "noproperties|system.Error"
End If
Catch ex As Exception
pString = New String(0) {}
pString(0) = "ERROR|" & ex.Message
End Try
Return pString
End Function
History
- 20th November, 2014: Initial version