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 2

From Wiki

Jump to: navigation, search

So the first part was simple.

And I want to keep it simple for just a while.

I want to make the reflection part a bit easier for myself and for others.

So I made a Class with shared methods (one for now).

  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.                 Return CType(_Object.GetType.GetProperty(PropertyName , Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(ObjectToReflect, Nothing), T)
  21.             End Function
  22.     #End Region
  23.  
  24.     #Region " Private methods "
  25.  
  26.     #End Region
  27.  
  28.         End Class
  29.     End Namespace


As you can see, I use reflection so the users of this Class don't have to cast and so it is type safe.

This will make the test look a bit like this:

  1. ''' <summary>
  2.             '''
  3.             ''' </summary>
  4.             ''' <remarks></remarks>
  5.             <Test()> _
  6.             Public Sub TestReflectionGetValueForRemarks()
  7.                 Dim _Translated As Common.Interfaces.ITranslated
  8.                 _Translated = New Common.Translated
  9.                 _Translated.Remarks = "Test"
  10.                 Assert.AreEqual("Test", Common.GetPropertyByReflection(Of String, Model.KitManagement.Interfaces.ICondition).GetProperty("Remarks", _Translated))
  11.             End Sub


One line and typesafe, what more do you want? I know the K wasn't really necessary but it makes sure it gets the correct object.

Run it, and green again. Life can be so beautiful.

213 Rating: 2.8/5 (4 votes cast)