How to get all properties of an object and its auxiliary objects

I have a similar question to this , except that I need to encode all helper objects and change the property to read only

Please check my code below. This one loop is just the main object. When I tried to loop over my helper objects, I got an overflow.

Thank.



Public Class ReadOnlyTypeDescriptor 
    Inherits CustomTypeDescriptor 
    Private mComponent As Object 

    Public Sub New(ByVal component As Object) 
        MyBase.New(TypeDescriptor.GetProvider(component).GetTypeDescriptor(component)) 
        mComponent = component 
    End Sub 

    Public Overloads Overrides Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection 
        Dim inPdc As PropertyDescriptorCollection = MyBase.GetProperties(attributes) 

        Dim pdcs As PropertyDescriptor() = New PropertyDescriptor(inPdc.Count - 1) {} 
        For i As Integer = 0 To pdcs.Length - 1 
            If inPdc(i).IsReadOnly Then 
                pdcs(i) = inPdc(i) 
            Else 
                pdcs(i) = New ReadOnlyPropertyDescriptor(inPdc(i)) 
            End If 
        Next 

        Return New PropertyDescriptorCollection(pdcs, True) 
    End Function 

    Public Overloads Overrides Function GetProperties() As PropertyDescriptorCollection 
        Return GetProperties(Nothing) 
    End Function 

    Private Class ReadOnlyPropertyDescriptor 
        Inherits PropertyDescriptor 
        Private mParent As PropertyDescriptor 

        Public Sub New(ByVal parent As PropertyDescriptor) 
            MyBase.New(parent, New Attribute() {ReadOnlyAttribute.Yes}) 
            mParent = parent 
        End Sub 

        Public Overloads Overrides Function CanResetValue(ByVal component As Object) As Boolean 
            Return False 
            ' Read Only 
        End Function 

        Public Overloads Overrides ReadOnly Property ComponentType() As Type 
            Get 
                Return mParent.ComponentType 
            End Get 
        End Property 

        Public Overloads Overrides Function GetValue(ByVal component As Object) As Object 
            Return mParent.GetValue(component) 
        End Function 

        Public Overloads Overrides ReadOnly Property IsReadOnly() As Boolean 
            Get 
                Return True 
            End Get 
        End Property 

        Public Overloads Overrides ReadOnly Property PropertyType() As Type 
            Get 
                Return mParent.PropertyType 
            End Get 
        End Property 

        Public Overloads Overrides Sub ResetValue(ByVal component As Object) 
            ' Read Only 
        End Sub 

        Public Overloads Overrides Sub SetValue(ByVal component As Object, ByVal value As Object) 
            ' Read Only 
        End Sub 

        Public Overloads Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean 
            Return mParent.ShouldSerializeValue(component) 
        End Function 
    End Class 
End Class 


      

0


source to share


1 answer


If you have objects with circular references, you can visit properties more than once.



In such cases, I always find ways to "mark" those items that I have visited before, an idea commonly used when viewing graphs.

+1


source







All Articles