如何获取一个对象及其子对象的所有属性

发布于 2024-07-09 22:29:20 字数 2985 浏览 7 评论 0原文

我有一个类似的问题 这个除了我需要循环所有子对象并将属性更改为只读

请检查下面的代码。 这一个仅循环主要对象。 当我尝试循环其子对象时,出现溢出。

谢谢。



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 

I have a similar question to this one except I need to loop all the sub objects and change the property to read-only

Please check my code below. This one loop only the main object. When I tried to loop its sub objects, I got an overflow.

Thanks.



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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-07-16 22:29:20

如果您有带有循环引用的对象,您可能会多次访问属性。

在这种情况下,我总是想方设法“标记”我之前访问过的项目,这是遍历图形时常用的想法。

If you have objects with circular references, you might be visiting properties more than once.

In cases like this, I always find ways to 'mark' which items I've visited before, an idea which is commonly used when traversing graphs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文