13028


お世話になります。

VisualBasic.Netで既存のDLLコントロールの利用を考えております。  
コントロールに対してプロパティグリッドでダイナミックに変更出来るようにしたいのですが編集してほしくない、見てほしくないプロパティもあり、それらを隠ぺいしたく考えています。

継承してプロパティをオーバーライドしてをつける事も考えてみました。

この時にGet Setメソッドを書かずプロパティの定義部分にをつける事で隠ぺい可能でしょうか？

よろしくお願いします。


CC BY-SA 3.0
2015-08-04T03:35:44.950
13012
2015-08-04T11:09:31.077
10607
question
windows vb.net
既存DLLのプロパティを隠ぺい方法


以下のように`PropertyDescriptor`を`PropertyGrid`から要求されても渡さないようにすれば表示されないようにできます。  
また、プロパティへのアクセスが`PropertyDescriptor`経由であることを利用すれば、全く異なるオブジェクトのプロパティを表示させることも可能です。  
これらの方法を使うことで、元のオブジェクトに手を加えること無しに表示/非表示を変更できるようになります。

[code]

    Imports System.ComponentModel
    Public Class Form1
        Sub New()
    
            Dim pgs As PropertyGrid() = {New PropertyGrid(), New PropertyGrid(), New PropertyGrid(), New PropertyGrid()}
    
            pgs(0).SelectedObject = New TestClass0()
            AddHandler pgs(0).PropertyValueChanged, Sub()
                                                        pgs(3).Refresh()
                                                    End Sub
    
            pgs(1).SelectedObject = New TestClass1()
    
            pgs(2).SelectedObject = New TestClass2()
    
            pgs(3).SelectedObject = New Proxy(pgs(0).SelectedObject)
            AddHandler pgs(3).PropertyValueChanged, Sub()
                                                        pgs(0).Refresh()
                                                    End Sub
    
            Dim table As New TableLayoutPanel()
            table.ColumnCount = pgs.Length
            table.Dock = DockStyle.Fill
            For Each pg As PropertyGrid In pgs
                pg.Dock = DockStyle.Fill
                table.Controls.Add(pg)
            Next
    
            Me.Controls.Clear()
            Me.Controls.Add(table)
            Me.Width = 600
        End Sub
    End Class
    
    Public Class TestClass0
        Public Property Hidden As Boolean
    
        Public Property Visible As Boolean
    End Class
    
    Public Class TestClass1
        <Browsable(False)> _
        Public Property Hidden As Boolean
    
        Public Property Visible As Boolean
    End Class
    
    <TypeConverter(GetType(TestClass2TypeConverter))> _
    Public Class TestClass2
        Public Property Hidden As Boolean
    
        Public Property Visible As Boolean
    End Class
    
    Public Class TestClass2TypeConverter
        Inherits TypeConverter
    
        Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean
            Return True
        End Function
    
        Public Overrides Function GetProperties(context As ITypeDescriptorContext, value As Object, attributes() As Attribute) As PropertyDescriptorCollection
            Dim properties = TypeDescriptor.GetProperties(value.GetType())
            Dim list As New List(Of PropertyDescriptor)
            If (properties IsNot Nothing) Then
                For Each pd As PropertyDescriptor In properties
                    If (pd.Name <> "Hidden") Then
                        list.Add(pd)
                    End If
                Next
            End If
            If (list.Count <> properties.Count) Then
                properties = New PropertyDescriptorCollection(list.ToArray())
            End If
            Return properties
        End Function
    End Class
    
    <TypeConverter(GetType(ProxyTypeConverter))> _
    Public Class Proxy
        Sub New(ByVal source As Object)
            Me._Source = source
        End Sub
    
        Public ReadOnly Property Source As Object
            Get
                Return _Source
            End Get
        End Property
        Private _Source As Object
    End Class
    
    Public Class ProxyTypeConverter
        Inherits TypeConverter
    
        Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean
            Return True
        End Function
    
        Public Overrides Function GetProperties(context As ITypeDescriptorContext, value As Object, attributes() As Attribute) As PropertyDescriptorCollection
            If (TypeOf value Is Proxy) Then
                Dim proxy As Proxy = DirectCast(value, Proxy)
                If (proxy.Source Is Nothing) Then
                    Return New PropertyDescriptorCollection(New PropertyDescriptor(0) {})
                Else
                    Dim properties = TypeDescriptor.GetProperties(proxy.Source.GetType())
                    Dim list As New List(Of PropertyDescriptor)
                    If (properties IsNot Nothing) Then
                        For Each pd As PropertyDescriptor In properties
                            If (pd.Name <> "Hidden") Then
                                list.Add(New ProxyDescriptor(pd))
                            End If
                        Next
                    End If
    
                    If (list.Count <> properties.Count) Then
                        properties = New PropertyDescriptorCollection(list.ToArray())
                    End If
                    Return properties
                End If
    
    
            Else
                Return TypeDescriptor.GetProperties(value.GetType())
            End If
    
        End Function
    
        Class ProxyDescriptor
            Inherits PropertyDescriptor
    
            Sub New(ByVal original As PropertyDescriptor)
                MyBase.New(original)
                _Original = original
            End Sub
    
            Private _Original As PropertyDescriptor
    
            Public Overrides Function CanResetValue(component As Object) As Boolean
                Return _Original.CanResetValue(CType(component, Proxy).Source)
            End Function
    
            Public Overrides ReadOnly Property ComponentType As Type
                Get
                    Return _Original.ComponentType
                End Get
            End Property
    
            Public Overrides Function GetValue(component As Object) As Object
                Return _Original.GetValue(CType(component, Proxy).Source)
            End Function
    
            Public Overrides ReadOnly Property IsReadOnly As Boolean
                Get
                    Return _Original.IsReadOnly
                End Get
            End Property
    
            Public Overrides ReadOnly Property PropertyType As Type
                Get
                    Return _Original.PropertyType
                End Get
            End Property
    
            Public Overrides Sub ResetValue(component As Object)
                _Original.ResetValue(CType(component, Proxy).Source)
            End Sub
    
            Public Overrides Sub SetValue(component As Object, value As Object)
                _Original.SetValue(CType(component, Proxy).Source, value)
            End Sub
    
            Public Overrides Function ShouldSerializeValue(component As Object) As Boolean
                Return _Original.ShouldSerializeValue(CType(component, Proxy).Source)
            End Function
        End Class
    End Class
    
[/code]


CC BY-SA 3.0
2015-08-04T11:03:01.743
13028
2015-08-04T11:09:31.077
2015-08-04T11:09:31.077
4926
4926
13012
answer