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 1
From Wiki
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.
- Namespace Common.Interfaces
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <CLSCompliant(True)> _
- Public Interface ITranslated
- ''' <summary>
- '''
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Property Id() As String
- ''' <summary>
- ''' This is the Description property.
- ''' </summary>
- ''' <value>Translation</value>
- ''' <returns>Translation</returns>
- ''' <remarks></remarks>
- Property Description() As Interfaces.ITranslation
- ''' <summary>
- ''' This is the Remarks property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks>Max length = 500</remarks>
- Property Remarks() As String
- End Interface
- End Namespace
Then of course I made the Class.
- Imports Common.Interfaces
- Namespace Common
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <CLSCompliant(True)> _
- Public Class Translated
- Implements ITranslated
- #Region " Private members "
- ''' <summary>
- ''' A local variable called _id of type String
- ''' </summary>
- ''' <remarks>Has Property Id</remarks>
- Private _id As String
- ''' <summary>
- ''' A local variable called _description of type Translation
- ''' </summary>
- ''' <remarks>Has Property Description</remarks>
- Private _description As Interfaces.ITranslation
- ''' <summary>
- ''' A local variable called _remarks of type String
- ''' </summary>
- ''' <remarks>Has Property Remarks</remarks>
- Private _remarks 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(New Common.Translation(), "")
- End Sub
- ''' <summary>
- ''' Constructor with all the fields
- ''' </summary>
- ''' <param name="Description">DataType = Translation</param>
- ''' <param name="Remarks">DataType = String</param>
- ''' <remarks></remarks>
- Public Sub New(ByVal Description As Interfaces.ITranslation, ByVal Remarks As String)
- _Log = TDB2007_Logging.Logging.Log.Instance
- _Log.LogDebug("Constructor - Initializing log")
- _Log.LogDebug("Constructor - Setting description")
- Me.Description = Description
- _Log.LogDebug("Constructor - Setting remarks")
- Me.Remarks = Remarks
- End Sub
- #End Region
- #Region " Public properties "
- ''' <summary>
- ''' This is the Id property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks></remarks>
- Public Overridable ReadOnly Property Id() As String Implements ITranslated.Id
- Get
- _Log.LogDebug("Property Id - Returning _Id")
- Return _id
- End Get
- End Property
- ''' <summary>
- ''' This is the Description property.
- ''' </summary>
- ''' <value>Translation</value>
- ''' <returns>Translation</returns>
- ''' <remarks></remarks>
- Public Overridable Property Description() As Interfaces.ITranslation Implements ITranslated.Description
- Get
- _Log.LogDebug("Property Description - Returning _Description")
- Return _description
- End Get
- Set(ByVal Value As Interfaces.ITranslation)
- _Log.LogDebug("Property Description - Setting _Description")
- _description = Value
- End Set
- End Property
- ''' <summary>
- ''' This is the Remarks property.
- ''' </summary>
- ''' <value>String</value>
- ''' <returns>String</returns>
- ''' <remarks>Max length = 500</remarks>
- Public Overridable Property Remarks() As String Implements ITranslated.Remarks
- Get
- _Log.LogDebug("Property Remarks - Returning _remarks")
- Return _remarks
- End Get
- Set(ByVal Value As String)
- _Log.LogDebug("Property Remarks - Setting _Remarks")
- _remarks = 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 ITranslated.ToString
- _Log.LogDebug("ToString - initializing 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 _Description isnot nothing")
- If _description IsNot Nothing Then
- _Log.LogDebug("ToString - _description isnot nothing")
- ReturnString.Append(" - [Description: " & _description.ToString() & "]")
- End If
- _Log.LogDebug("ToString - checking if _remarks isnot nothing")
- If _remarks IsNot Nothing Then
- _Log.LogDebug("ToString - _remarks isnot nothing")
- ReturnString.Append(" - [Remarks: " & _remarks.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 ITranslated.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._description.Equals(CType(obj, Translated)._description))
- Return Me._description.Equals(CType(obj, Translated)._description)
- 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 Overridable 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._description.CompareTo(CType(obj, Translated)._description))
- Return Me._description.CompareTo(CType(obj, Translated)._description)
- 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
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.
- Imports NUnit.Core
- Imports NUnit.Framework
- Namespace Common.Test
- <TestFixture(Description:="")> _
- Public Class TestTranslated
- #Region " Constructors "
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub New()
- End Sub
- #End Region
- #Region " Tests "
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- <Test()> _
- Public Sub TestReflectionGetValue()
- Dim _Translated As Common.Interfaces.ITranslated
- _Translated = New Common.Translated
- _Translated.Remarks = "Test"
- Dim _Object As Object = _Translated
- Assert.AreEqual("Test", _Object.GetType.GetProperty("Remarks", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public).GetValue(_Translated, Nothing))
- End Sub
- #End Region
- End Class
- End Namespace
And guess what? We get the green light and we become all ecstatic!



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