Answered by:
Programmatically Ignore Inherited Properties while iterating through object properties

Question
-
User1920326549 posted
Hi All,
I'm currently running a loop to iterate through an object properties:
Public Class Notification
Inherits CommonProp
Public Property NotificationID As Integer
Public Property Message As String
Public Property ReceivedDateTime As DateTime
End ClassHowever, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.
Is there anyway of ignoring the CommonProp properties?
Please kindly assist.
Thank you in advance.
Warmest Regards,
leroylll
Saturday, October 16, 2010 10:19 PM
Answers
-
User1224194097 posted
However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.You can use Reflection and check if the Declaring Type and Reflecting Type are same.
Public Class ParentClass Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class Public Class ChildClass Inherits ParentClass Private m_Number As String Public Property Number() As String Get Return m_Number End Get Set(ByVal value As String) m_Number = value End Set End Property End Class Dim cls As New ChildClass() cls.Number = "Test Number" Dim properties = cls.GetType().GetProperties() _ .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList() 'Get Values here Dim values = (From a In properties Select a.GetValue(cls, Nothing)).ToArray()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, October 17, 2010 12:25 AM -
User1920326549 posted
Just to add on, i found this code on http://snipplr.com/view/4648/add-item-to-array:
- // Add an item to the end of an existing array
- // create a temporary array with an extra slot
- // at the end
- // add the contents of the ar1 to ar2
- // at position 0
- ar1.CopyTo(ar2, 0);
- // add the desired value
- ar2.SetValue("Code.", ar1.Length);
- // overwrite ar1 with ar2 and voila!
- // the contents of ar1 should now be {"I", "Like", "To", "Code."}
- ar1 = ar2;
Hope it will be useful to those reading.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 21, 2010 10:19 AM
All replies
-
User1224194097 posted
However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.You can use Reflection and check if the Declaring Type and Reflecting Type are same.
Public Class ParentClass Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class Public Class ChildClass Inherits ParentClass Private m_Number As String Public Property Number() As String Get Return m_Number End Get Set(ByVal value As String) m_Number = value End Set End Property End Class Dim cls As New ChildClass() cls.Number = "Test Number" Dim properties = cls.GetType().GetProperties() _ .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList() 'Get Values here Dim values = (From a In properties Select a.GetValue(cls, Nothing)).ToArray()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, October 17, 2010 12:25 AM -
User1920326549 posted
However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.How are you iterating through properties? Are you using Reflection? If so, you can check if the Declaring Type and Reflecting Type are same.
- Public Class ParentClass
- Private m_Name As String
- Public Property Name() As String
- Get
- Return m_Name
- End Get
- Set(ByVal value As String)
- m_Name = value
- End Set
- End Property
- End Class
- Public Class ChildClass
- Inherits ParentClass
- Private m_Number As String
- Public Property Number() As String
- Get
- Return m_Number
- End Get
- Set(ByVal value As String)
- m_Number = value
- End Set
- End Property
- End Class
Public Class ParentClass Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class Public Class ChildClass Inherits ParentClass Private m_Number As String Public Property Number() As String Get Return m_Number End Get Set(ByVal value As String) m_Number = value End Set End Property End Class
- Dim cls As New ChildClass()
- cls.Number = "Test Number"
- Dim properties = cls.GetType().GetProperties() _
- .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList()
- 'Get Value here
- Dim strNumber = properties.First().GetValue(cls, Nothing)
Dim cls As New ChildClass() cls.Number = "Test Number" Dim properties = cls.GetType().GetProperties() _ .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList() 'Get Value here Dim strNumber = properties.First().GetValue(cls, Nothing)
Correct me if I had understood your requirement wrong.
yup.... im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.
Sunday, October 17, 2010 4:16 AM -
User1224194097 posted
im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.Did you try my code?
Sunday, October 17, 2010 10:21 PM -
User1920326549 posted
im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.Did you try my code?
Yup... thanks.... it worked.
Tuesday, October 19, 2010 11:55 AM -
User1920326549 posted
Just to add on, i found this code on http://snipplr.com/view/4648/add-item-to-array:
- // Add an item to the end of an existing array
- // create a temporary array with an extra slot
- // at the end
- // add the contents of the ar1 to ar2
- // at position 0
- ar1.CopyTo(ar2, 0);
- // add the desired value
- ar2.SetValue("Code.", ar1.Length);
- // overwrite ar1 with ar2 and voila!
- // the contents of ar1 should now be {"I", "Like", "To", "Code."}
- ar1 = ar2;
Hope it will be useful to those reading.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 21, 2010 10:19 AM