Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Getting a property using reflection part 4
From Wiki
So I want a bit more now. I want to get the EN property from the description directly from the translated object. So in good old TDD tradition I come up with this test.
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <Test()> _
- Public Sub TestReflectionGetValueForDescriptionEn()
- Dim _Translated As Common.Interfaces.ITranslated
- _Translated = New Common.Translated
- _Translated .Description.En = "Test"
- Assert.AreEqual("Test", Common.GetPropertyByReflection(Of String, Common.Interfaces.ITranslated ).GetProperty("Description.En", _Translated ))
- End Sub
And this gives us a red light of course.
So I had to change the GetPropertyByReflection to use some recursion and this is it.
- Namespace Common
- ''' <summary>
- '''
- ''' </summary>
- ''' <typeparam name="T"></typeparam>
- ''' <typeparam name="K"></typeparam>
- ''' <remarks></remarks>
- Public Class GetPropertyByReflection(Of T, K)
- #Region " Public methods "
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="PropertyName"></param>
- ''' <param name="ObjectToReflect"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function GetProperty(ByVal PropertyName As String, ByVal ObjectToReflect As K) As T
- Dim _Object As Object = ObjectToReflect
- Dim _Result As T
- If Not PropertyName.Contains(".") Then
- _Result = CType(_Object.GetType.GetProperty(PropertyName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(ObjectToReflect, Nothing), T)
- Else
- _Result = CType(GetPropertyWithRecursion(PropertyName, ObjectToReflect), T)
- End If
- Return _Result
- End Function
- #End Region
- #Region " Private methods "
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="propertyName"></param>
- ''' <param name="objectToReflect"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Private Shared Function GetPropertyWithRecursion(ByVal propertyName As String, ByVal objectToReflect As Object) As Object
- Dim _returnObject As Object
- Dim _Object As Object = objectToReflect
- If propertyName.Contains(".") Then
- _returnObject = GetPropertyWithRecursion(propertyName.Substring(propertyName.IndexOf(".") + 1), _Object.GetType.GetProperty(propertyName.Substring(0, propertyName.IndexOf(".")), Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(objectToReflect, Nothing))
- Else
- _returnObject = _Object.GetType.GetProperty(propertyName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(objectToReflect, Nothing)
- End If
- Return _returnObject
- End Function
- #End Region
- End Class
- End Namespace
And now the tests all run green and life looks even better.



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.