【问题标题】:VB.NET Dynamic CustomTypeDescriptorVB.NET 动态自定义类型描述符
【发布时间】:2010-10-01 20:39:22
【问题描述】:

我正在玩弄一个想法(以前从未使用过 TypeDescriptors),并设法让它很好地工作。但我担心我在小实验中做出的一些“最佳实践”决定。

我使用 CustomTypeDescriptor,它从其 PropertyDescriptor 接收一个事件,指示值正在更改或被查询。

每次调用 GetTypeDescriptor 时,TypeDescriptorProvider 都会生成一个全新的 CustomTypeDescriptor 实例,然后将 CustomTypeDescriptor 上的事件绑定到实例对象。

我不确定每次调用 GetTypeDescriptor 时是否生成一个新的 CustomTypeDescriptor 是个好主意(尽管我必须这样做才能让它工作)。我也不确定将事件直接从 CustomTypeDescriptor 绑定到实例对象是否有任何后果,特别是如果 CustomTypeDescriptor 是动态的。

你们怎么看?下面是我的 Provider 的示例代码:

Class EntityTypeDescriptionProvider
    Inherits TypeDescriptionProvider

Public Sub New(ByVal parent As TypeDescriptionProvider)
    MyBase.New(parent)
End Sub
Protected Sub New()

End Sub

Public Overrides Function GetTypeDescriptor(ByVal objectType As Type, ByVal instance As Object) As ICustomTypeDescriptor
    Dim _CustomTypeDescriptor As EntityTypeDescriptor

    'Grabbin the base descriptor.
    Dim Descriptor As ICustomTypeDescriptor = MyBase.GetTypeDescriptor(objectType, Nothing)

    'If for...whatever reason the instance is empty, return the default descriptor for the type.
    If instance Is Nothing Then
        Return Descriptor
    End If

    'If the instance doesnt implement the interface I use for Descriptor customization
    '(which should never happen) return the default descriptor for the type.
    If Not Functions.IsImplemented(instance.GetType, GetType(ICustomTypeDescriptorEntity)) Then
        Return Descriptor
    End If

    '
    '
    'some lengthy "customization" based on the state of the instance.
    '
    '

    AddHandler _CustomTypeDescriptor.GetValue, AddressOf CType(instance, ICustomTypeDescriptorEntity).GetValue
    AddHandler _CustomTypeDescriptor.SetValue, AddressOf CType(instance, ICustomTypeDescriptorEntity).SetValue

    Return _CustomTypeDescriptor
End Function
End Class

【问题讨论】:

    标签: vb.net typedescriptor icustomtypedescriptor


    【解决方案1】:

    我已经用了一段时间了,它根本没有爆炸。

    仍然欢迎反馈,我正在回答这个问题。

    【讨论】:

      猜你喜欢
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 2022-01-19
      • 2020-01-23
      • 1970-01-01
      • 2016-11-17
      • 2011-10-12
      相关资源
      最近更新 更多