Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

Getting a property using reflection part 4

From Wiki

Jump to: navigation, search

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.

  1. ''' <summary>
  2.             '''
  3.             ''' </summary>
  4.             ''' <remarks></remarks>
  5.             <Test()> _
  6.             Public Sub TestReflectionGetValueForDescriptionEn()
  7.                 Dim _Translated As Common.Interfaces.ITranslated
  8.                 _Translated = New Common.Translated
  9.                 _Translated .Description.En = "Test"
  10.                 Assert.AreEqual("Test", Common.GetPropertyByReflection(Of String, Common.Interfaces.ITranslated ).GetProperty("Description.En", _Translated ))
  11.             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.

  1. Namespace Common
  2.         ''' <summary>
  3.         '''
  4.         ''' </summary>
  5.         ''' <typeparam name="T"></typeparam>
  6.         ''' <typeparam name="K"></typeparam>
  7.         ''' <remarks></remarks>
  8.         Public Class GetPropertyByReflection(Of T, K)
  9.  
  10.     #Region " Public methods "
  11.             ''' <summary>
  12.             '''
  13.             ''' </summary>
  14.             ''' <param name="PropertyName"></param>
  15.             ''' <param name="ObjectToReflect"></param>
  16.             ''' <returns></returns>
  17.             ''' <remarks></remarks>
  18.             Public Shared Function GetProperty(ByVal PropertyName As String, ByVal ObjectToReflect As K) As T
  19.                 Dim _Object As Object = ObjectToReflect
  20.                 Dim _Result As T
  21.                 If Not PropertyName.Contains(".") Then
  22.                     _Result = CType(_Object.GetType.GetProperty(PropertyName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(ObjectToReflect, Nothing), T)
  23.                 Else
  24.                     _Result = CType(GetPropertyWithRecursion(PropertyName, ObjectToReflect), T)
  25.                 End If
  26.                 Return _Result
  27.             End Function
  28.     #End Region
  29.  
  30.     #Region " Private methods "
  31.             ''' <summary>
  32.             '''
  33.             ''' </summary>
  34.             ''' <param name="propertyName"></param>
  35.             ''' <param name="objectToReflect"></param>
  36.             ''' <returns></returns>
  37.             ''' <remarks></remarks>
  38.             Private Shared Function GetPropertyWithRecursion(ByVal propertyName As String, ByVal objectToReflect As Object) As Object
  39.                 Dim _returnObject As Object
  40.                 Dim _Object As Object = objectToReflect
  41.                 If propertyName.Contains(".") Then
  42.                     _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))
  43.                 Else
  44.                     _returnObject = _Object.GetType.GetProperty(propertyName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(objectToReflect, Nothing)
  45.                 End If
  46.                 Return _returnObject
  47.             End Function
  48.     #End Region
  49.  
  50.         End Class
  51.     End Namespace


And now the tests all run green and life looks even better.

215 Rating: 2.4/5 (5 votes cast)