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 3

From Wiki

Jump to: navigation, search

So to proove my point that the generics do work, I made this unit test.

  1. ''' <summary>
  2.             '''
  3.             ''' </summary>
  4.             ''' <remarks></remarks>
  5.             <Test()> _
  6.             Public Sub TestReflectionGetValueDescription()
  7.                 Dim _Translated As Common.Interfaces.ITranslated
  8.                 _Translated = New Common.Translated
  9.                 Dim _Description As Common.Interfaces.ITranslation
  10.                 _Description = New Common.Translation
  11.                 _Description.En = "TestEn"
  12.                 _Description.Nl = "TestNl"
  13.                 _Description.Fr = "TestFr"
  14.                 _Condition.Description = _Description
  15.                 Assert.AreEqual(_Description, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ))
  16.                 Assert.AreEqual(_Description.En, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).En)
  17.                 Assert.AreEqual(_Description.Fr, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).Fr)
  18.                 Assert.AreEqual(_Description.Nl, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).Nl)
  19.             End Sub


And voilĂ , another green light.

This is the Itranslation interface.

  1. Namespace Common.Interfaces
  2.         ''' <summary>
  3.         '''
  4.         ''' </summary>
  5.         ''' <remarks></remarks>
  6.         <CLSCompliant(True)> _
  7.         Public Interface ITranslation
  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 En property.
  17.             ''' </summary>
  18.             ''' <value>String</value>
  19.             ''' <returns>String</returns>
  20.             ''' <remarks></remarks>
  21.             Property En() As String
  22.             ''' <summary>
  23.             ''' This is the Nl property.
  24.             ''' </summary>
  25.             ''' <value>String</value>
  26.             ''' <returns>String</returns>
  27.             ''' <remarks></remarks>
  28.             Property Nl() As String
  29.             ''' <summary>
  30.             ''' This is the Fr property.
  31.             ''' </summary>
  32.             ''' <value>String</value>
  33.             ''' <returns>String</returns>
  34.             ''' <remarks></remarks>
  35.             Property Fr() As String
  36.         End Interface
  37.     End Namespace


And this is the implementation of that Interface.

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

214 Rating: 2.3/5 (4 votes cast)