首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取对象及其子对象的所有属性

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

Stack Overflow用户
提问于 2008-12-15 04:16:55
回答 1查看 1.7K关注 0票数 0

除了需要循环所有子对象并将属性更改为只读之外,我还有一个类似于this one的问题

请检查我下面的代码。这一个只循环主对象。当我试图循环它的子对象时,我得到了一个溢出。

谢谢。

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

发布于 2008-12-15 05:44:41

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

在这样的情况下,我总是找到方法来“标记”我以前访问过的项目,这是在遍历图时通常使用的想法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/367462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档