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 1

From Wiki

Jump to: navigation, search

Let's start with a simple case. ;-)

But not that simple. :)

Since I like to use interfaces as much as possible, I made an interface for this Class as well. See below.

  1. Namespace Common.Interfaces
  2.         ''' <summary>
  3.         '''
  4.         ''' </summary>
  5.         ''' <remarks></remarks>
  6.         <CLSCompliant(True)> _
  7.         Public Interface ITranslated
  8.             ''' <summary>
  9.             '''
  10.             ''' </summary>
  11.             ''' <value></value>
  12.             ''' <returns></returns>
  13.             ''' <remarks></remarks>
  14.             Property Id() As String
  15.             ''' <summary>
  16.             ''' This is the Description property.
  17.             ''' </summary>
  18.             ''' <value>Translation</value>
  19.             ''' <returns>Translation</returns>
  20.             ''' <remarks></remarks>
  21.             Property Description() As Interfaces.ITranslation
  22.             ''' <summary>
  23.             ''' This is the Remarks property.
  24.             ''' </summary>
  25.             ''' <value>String</value>
  26.             ''' <returns>String</returns>
  27.             ''' <remarks>Max length = 500</remarks>
  28.             Property Remarks() As String
  29.         End Interface
  30.     End Namespace


Then of course I made the Class.

  1. Imports Common.Interfaces
  2.  
  3.     Namespace Common
  4.         ''' <summary>
  5.         '''
  6.         ''' </summary>
  7.         ''' <remarks></remarks>
  8.         <CLSCompliant(True)> _
  9.         Public Class Translated
  10.             Implements ITranslated
  11.  
  12.     #Region " Private members "
  13.             ''' <summary>
  14.             ''' A local variable called _id of type String
  15.             ''' </summary>
  16.             ''' <remarks>Has Property Id</remarks>
  17.             Private _id As String
  18.             ''' <summary>
  19.             ''' A local variable called _description of type Translation
  20.             ''' </summary>
  21.             ''' <remarks>Has Property Description</remarks>
  22.             Private _description As Interfaces.ITranslation
  23.             ''' <summary>
  24.             ''' A local variable called _remarks of type String
  25.             ''' </summary>
  26.             ''' <remarks>Has Property Remarks</remarks>
  27.             Private _remarks As String
  28.             ''' <summary>
  29.             ''' A local variable called _Log of type TDB2007_Logging.Logging.Log
  30.             ''' </summary>
  31.             ''' <remarks>Is initialized in the constructor.</remarks>
  32.             Private _Log As TDB2007_Logging.Logging.Log
  33.     #End Region
  34.  
  35.     #Region " Constructors "
  36.             ''' <summary>
  37.             ''' Default empty constructor
  38.             ''' </summary>
  39.             ''' <remarks></remarks>
  40.             Public Sub New()
  41.                 Me.New(New Common.Translation(), "")
  42.             End Sub
  43.  
  44.             ''' <summary>
  45.             ''' Constructor with all the fields
  46.             ''' </summary>
  47.             ''' <param name="Description">DataType = Translation</param>
  48.             ''' <param name="Remarks">DataType = String</param>
  49.             ''' <remarks></remarks>
  50.             Public Sub New(ByVal Description As Interfaces.ITranslation, ByVal Remarks As String)
  51.                 _Log = TDB2007_Logging.Logging.Log.Instance
  52.                 _Log.LogDebug("Constructor - Initializing log")
  53.                 _Log.LogDebug("Constructor - Setting description")
  54.                 Me.Description = Description
  55.                 _Log.LogDebug("Constructor - Setting remarks")
  56.                 Me.Remarks = Remarks
  57.             End Sub
  58.     #End Region
  59.  
  60.     #Region " Public properties "
  61.             ''' <summary>
  62.             ''' This is the Id property.
  63.             ''' </summary>
  64.             ''' <value>String</value>
  65.             ''' <returns>String</returns>
  66.             ''' <remarks></remarks>
  67.             Public Overridable ReadOnly Property Id() As String Implements ITranslated.Id
  68.                 Get
  69.                     _Log.LogDebug("Property Id - Returning _Id")
  70.                     Return _id
  71.                 End Get
  72.             End Property
  73.  
  74.             ''' <summary>
  75.             ''' This is the Description property.
  76.             ''' </summary>
  77.             ''' <value>Translation</value>
  78.             ''' <returns>Translation</returns>
  79.             ''' <remarks></remarks>
  80.             Public Overridable Property Description() As Interfaces.ITranslation Implements ITranslated.Description
  81.                 Get
  82.                     _Log.LogDebug("Property Description - Returning _Description")
  83.                     Return _description
  84.                 End Get
  85.                 Set(ByVal Value As Interfaces.ITranslation)
  86.                     _Log.LogDebug("Property Description - Setting _Description")
  87.                     _description = Value
  88.                 End Set
  89.             End Property
  90.  
  91.             ''' <summary>
  92.             ''' This is the Remarks property.
  93.             ''' </summary>
  94.             ''' <value>String</value>
  95.             ''' <returns>String</returns>
  96.             ''' <remarks>Max length = 500</remarks>
  97.            Public Overridable Property Remarks() As String Implements ITranslated.Remarks
  98.                 Get
  99.                     _Log.LogDebug("Property Remarks - Returning _remarks")
  100.                     Return _remarks
  101.                 End Get
  102.                 Set(ByVal Value As String)
  103.                     _Log.LogDebug("Property Remarks - Setting _Remarks")
  104.                     _remarks = Value
  105.                 End Set
  106.             End Property
  107.     #End Region
  108.  
  109.     #Region " Overrides ToString "
  110.             ''' <summary>
  111.             ''' Overridden ToString method for this class
  112.             ''' </summary>
  113.             ''' <returns>String</returns>
  114.             ''' <remarks>The String value of this class represented by the ToStrings of all it's private members</remarks>
  115.             Public Overrides Function ToString() As String Implements ITranslated.ToString
  116.                 _Log.LogDebug("ToString - initializing returnstring")
  117.                 Dim ReturnString As New System.Text.StringBuilder()
  118.                 _Log.LogDebug("ToString - Checking if _id isnot nothing")
  119.                 If _id IsNot Nothing Then
  120.                     _Log.LogDebug("ToString - _id isnot nothing")
  121.                     ReturnString.Append("[Id: " & _id.ToString() & "]")
  122.                 End If
  123.                 _Log.LogDebug("ToString - Checking if _Description isnot nothing")
  124.                 If _description IsNot Nothing Then
  125.                     _Log.LogDebug("ToString - _description isnot nothing")
  126.                     ReturnString.Append(" - [Description: " & _description.ToString() & "]")
  127.                 End If
  128.                 _Log.LogDebug("ToString - checking if _remarks isnot nothing")
  129.                 If _remarks IsNot Nothing Then
  130.                     _Log.LogDebug("ToString - _remarks isnot nothing")
  131.                     ReturnString.Append(" - [Remarks: " & _remarks.ToString() & "]")
  132.                 End If
  133.                 _Log.LogDebug("ToString - Returning ReturnString: " & ReturnString.ToString)
  134.                 Return ReturnString.ToString()
  135.             End Function
  136.     #End Region
  137.  
  138.     #Region " Overrides Equals "
  139.             ''' <summary>
  140.             ''' Overridden Equals method for this class
  141.             ''' </summary>
  142.             ''' <returns>Boolean</returns>
  143.             ''' <remarks>The Boolean value indictaing if this is equal to the other class</remarks>
  144.             Public Overrides Function Equals(ByVal obj As Object) As Boolean Implements ITranslated.Equals
  145.                 _Log.LogDebug("Equals - Checking if obj isnot nothing")
  146.                 If obj IsNot Nothing Then
  147.                     _Log.LogDebug("Equals - Checking if obj is of correct type")
  148.                     If obj.GetType Is Me.GetType Then
  149.                         _Log.LogDebug("Equals - obj is of correct type")
  150.                         _Log.LogDebug("Equals - Returning: " & Me._description.Equals(CType(obj, Translated)._description))
  151.                         Return Me._description.Equals(CType(obj, Translated)._description)
  152.                     Else
  153.                         _Log.LogDebug("Equals - obj is of incorrect type")
  154.                         _Log.LogDebug("Equals - returning false")
  155.                         Return False
  156.                     End If
  157.                 Else
  158.                     _Log.LogDebug("Equals - obj is nothing")
  159.                     Return False
  160.                 End If
  161.             End Function
  162.     #End Region
  163.  
  164.     #Region " Overrides CompareTo "
  165.             ''' <summary>
  166.             ''' Overridden CompareTo method for this class
  167.             ''' </summary>
  168.             ''' <returns>Integer</returns>
  169.             ''' <remarks></remarks>
  170.             Public Overridable Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
  171.                 _Log.LogDebug("CompareTo - Checking if obj isnot nothing")
  172.                 If obj IsNot Nothing Then
  173.                     _Log.LogDebug("CompareTo - Checking if obj is correct type")
  174.                     If obj.GetType Is Me.GetType Then
  175.                         _Log.LogDebug("CompareTo - obj is correct type")
  176.                         _Log.LogDebug("CompareTo - returning: " & Me._description.CompareTo(CType(obj, Translated)._description))
  177.                         Return Me._description.CompareTo(CType(obj, Translated)._description)
  178.                     Else
  179.                         _Log.LogDebug("CompareTo - obj is not correct type")
  180.                         _Log.LogDebug("CompareTo - returning -1")
  181.                         Return -1
  182.                     End If
  183.                 Else
  184.                     _Log.LogDebug("CompareTo - obj is nothing")
  185.                     Return -1
  186.                 End If
  187.             End Function
  188.     #End Region
  189.  
  190.         End Class
  191.     End Namespace


Which is pretty basic.

Then I made a little test case to see if I could get the property. And this is where it gets simple, because I wanted the value for Remarks.

  1. Imports NUnit.Core
  2.     Imports NUnit.Framework
  3.  
  4.     Namespace Common.Test
  5.         <TestFixture(Description:="")> _
  6.         Public Class TestTranslated
  7.  
  8.     #Region " Constructors "
  9.             ''' <summary>
  10.             '''
  11.             ''' </summary>
  12.             ''' <remarks></remarks>
  13.             Public Sub New()
  14.  
  15.             End Sub
  16.     #End Region
  17.  
  18.     #Region " Tests "
  19.             ''' <summary>
  20.             '''
  21.             ''' </summary>
  22.             ''' <remarks></remarks>
  23.             <Test()> _
  24.             Public Sub TestReflectionGetValue()
  25.                 Dim _Translated As Common.Interfaces.ITranslated
  26.                 _Translated = New Common.Translated
  27.                 _Translated.Remarks = "Test"
  28.                 Dim _Object As Object = _Translated
  29.                 Assert.AreEqual("Test", _Object.GetType.GetProperty("Remarks", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(_Translated, Nothing))
  30.             End Sub
  31.     #End Region
  32.  
  33.         End Class
  34.     End Namespace


And guess what? We get the green light and we become all ecstatic!

212 Rating: 3.2/5 (5 votes cast)