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 3
From Wiki
So to proove my point that the generics do work, I made this unit test.
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <Test()> _
- Public Sub TestReflectionGetValueDescription()
- Dim _Translated As Common.Interfaces.ITranslated
- _Translated = New Common.Translated
- Dim _Description As Common.Interfaces.ITranslation
- _Description = New Common.Translation
- _Description.En = "TestEn"
- _Description.Nl = "TestNl"
- _Description.Fr = "TestFr"
- _Condition.Description = _Description
- Assert.AreEqual(_Description, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ))
- Assert.AreEqual(_Description.En, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).En)
- Assert.AreEqual(_Description.Fr, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).Fr)
- Assert.AreEqual(_Description.Nl, Common.GetPropertyByReflection(Of Common.Interfaces.ITranslation, Common.Interfaces.ITranslated).GetProperty("Description", _Translated ).Nl)
- End Sub
And voilĂ , another green light.
This is the Itranslation interface.
- Namespace Common.Interfaces
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <CLSCompliant(True)> _
- Public Interface ITranslation
- ''' <summary>
- '''
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Property Id() As String
- ''' <summary>
- ''' This is the En property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Property En() As String
- ''' <summary>
- ''' This is the Nl property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Property Nl() As String
- ''' <summary>
- ''' This is the Fr property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Property Fr() As String
- End Interface
- End Namespace
And this is the implementation of that Interface.
- Imports Common.Interfaces
- Namespace Common
- ''' <summary>
- ''' The translation class is made to translate translatable parameters.
- ''' It is made so that we can easily add another language.
- ''' </summary>
- ''' <remarks></remarks>
- <CLSCompliant(True)> _
- Public Class Translation
- Implements ITranslation
- #Region " Private members "
- ''' <summary>
- ''' A local variable called _id of type Integer
- ''' </summary>
- ''' <remarks>Has Property Id</remarks>
- Private _id As String
- ''' <summary>
- ''' A local variable called _en of type String
- ''' </summary>
- ''' <remarks>Has Property En</remarks>
- Private _en As String
- ''' <summary>
- ''' A local variable called _nl of type String
- ''' </summary>
- ''' <remarks>Has Property Nl</remarks>
- Private _nl As String
- ''' <summary>
- ''' A local variable called _fr of type String
- ''' </summary>
- ''' <remarks>Has Property Fr</remarks>
- Private _fr As String
- ''' <summary>
- ''' A local variable called _Log of type TDB2007_Logging.Logging.Log
- ''' </summary>
- ''' <remarks>Is initialized in the constructor.</remarks>
- Private _Log As TDB2007_Logging.Logging.Log
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Default empty constructor
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub New()
- Me.New("", "", "")
- End Sub
- ''' <summary>
- ''' Constructor with all the fields
- ''' </summary>
- ''' <param name="En">DataType = String</param>
- ''' <param name="Nl">DataType = String</param>
- ''' <param name="Fr">DataType = String</param>
- ''' <remarks></remarks>
- Public Sub New(ByVal En As String, ByVal Nl As String, ByVal Fr As String)
- _Log = TDB2007_Logging.Logging.Log.Instance
- _Log.LogDebug("Constructor - Initializing log")
- _Log.LogDebug("Constructor - setting en")
- Me.En = En
- _Log.LogDebug("Constructor - setting nl")
- Me.Nl = Nl
- _Log.LogDebug("Constructor - setting fr")
- Me.Fr = Fr
- End Sub
- #End Region
- #Region " Public properties "
- ''' <summary>
- ''' This is the Id property.
- ''' </summary>
- ''' <value>Integer</value>
- ''' <returns>Integer</returns>
- ''' <remarks></remarks>
- Public Overridable ReadOnly Property Id() As String Implements ITranslation.Id
- Get
- _Log.LogDebug("Property id - Returning _Id")
- Return _id
- End Get
- End Property
- ''' <summary>
- ''' This is the En property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Public Overridable Property En() As String Implements ITranslation.En
- Get
- _Log.LogDebug("Property En - Returning _En")
- Return _en
- End Get
- Set(ByVal Value As String)
- _Log.LogDebug("Property En - Setting _En")
- _en = Value
- End Set
- End Property
- ''' <summary>
- ''' This is the Nl property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Public Overridable Property Nl() As String Implements ITranslation.Nl
- Get
- _Log.LogDebug("Property Nl - Returning _Nl")
- Return _nl
- End Get
- Set(ByVal Value As String)
- _Log.LogDebug("Property NL - Setting _Nl")
- _nl = Value
- End Set
- End Property
- ''' <summary>
- ''' This is the Fr property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Public Overridable Property Fr() As String Implements ITranslation.Fr
- Get
- _Log.LogDebug("Property Fr - Returning _Fr")
- Return _fr
- End Get
- Set(ByVal Value As String)
- _Log.LogDebug("Property Fr - Setting _Fr")
- _fr = Value
- End Set
- End Property
- #End Region
- #Region " Overrides ToString "
- ''' <summary>
- ''' Overridden ToString method for this class
- ''' </summary>
- ''' <returns>String</returns>
- ''' <remarks>The String value of this class represented by the ToStrings of all it's private members</remarks>
- Public Overrides Function ToString() As String Implements ITranslation.ToString
- _Log.LogDebug("ToString - intiializing returnstring")
- Dim ReturnString As New System.Text.StringBuilder()
- _Log.LogDebug("ToString - Checking if _id isnot nothing")
- If _id IsNot Nothing Then
- _Log.LogDebug("ToString - _id isnot nothing")
- ReturnString.Append("[Id: " & _id.ToString() & "]")
- End If
- _Log.LogDebug("ToString - Checking if _en isnot nothing")
- If _en IsNot Nothing Then
- _Log.LogDebug("ToString - _en isnot nothing")
- ReturnString.Append(" - [En: " & _en.ToString() & "]")
- End If
- _Log.LogDebug("ToString - checking if _nl isnot nothing")
- If _nl IsNot Nothing Then
- _Log.LogDebug("ToString - _nl isnot nothing")
- ReturnString.Append(" - [Nl: " & _nl.ToString() & "]")
- End If
- _Log.LogDebug("ToString - checking if _fr isnot nothing")
- If _fr IsNot Nothing Then
- _Log.LogDebug("ToString - _fr isnot nothing")
- ReturnString.Append(" - [Fr: " & _fr.ToString() & "]")
- End If
- _Log.LogDebug("ToString - returning returnstring: " & ReturnString.ToString)
- Return ReturnString.ToString()
- End Function
- #End Region
- #Region " Overrides Equals "
- ''' <summary>
- ''' Overridden Equals method for this class
- ''' </summary>
- ''' <returns>Boolean</returns>
- ''' <remarks>The Boolean value indictaing if this is equal to the other class</remarks>
- Public Overrides Function Equals(ByVal obj As Object) As Boolean Implements ITranslation.Equals
- _Log.LogDebug("Equals - Checking if obj isnot nothing")
- If obj IsNot Nothing Then
- _Log.LogDebug("Equals - Checking if obj is of correct type")
- If obj.GetType Is Me.GetType Then
- _Log.LogDebug("Equals - obj is of correct type")
- _Log.LogDebug("Equals - Returning: " & Me._en.Equals(CType(obj, Translation)._en))
- Return Me._en.Equals(CType(obj, Translation)._en)
- Else
- _Log.LogDebug("Equals - obj is of incorrect type")
- _Log.LogDebug("Equals - returning false")
- Return False
- End If
- Else
- _Log.LogDebug("Equals - obj is nothing")
- Return False
- End If
- End Function
- #End Region
- #Region " Overrides CompareTo "
- ''' <summary>
- ''' Overridden CompareTo method for this class
- ''' </summary>
- ''' <returns>Integer</returns>
- ''' <remarks></remarks>
- Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
- _Log.LogDebug("CompareTo - Checking if obj isnot nothing")
- If obj IsNot Nothing Then
- _Log.LogDebug("CompareTo - Checking if obj is correct type")
- If obj.GetType Is Me.GetType Then
- _Log.LogDebug("CompareTo - obj is correct type")
- _Log.LogDebug("CompareTo - returning: " & Me._en.CompareTo(CType(obj, Translation)._en))
- Return Me._en.CompareTo(CType(obj, Translation)._en)
- Else
- _Log.LogDebug("CompareTo - obj is not correct type")
- _Log.LogDebug("CompareTo - returning -1")
- Return -1
- End If
- Else
- _Log.LogDebug("CompareTo - obj is nothing")
- Return -1
- End If
- End Function
- #End Region
- End Class
- End Namespace



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