0

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

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

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

我不确定每次调用 GetTypeDescriptor 时是否生成一个新的 CustomTypeDescriptor 是否是一个好主意(即使我不得不这样做)。我也不确定直接从 CustomTypeDescriptor 绑定事件到实例对象是否有任何后果,特别是如果 CustomTypeDescriptor 是动态的。

你们有什么感想?我的提供者的示例代码如下:

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
4

1 回答 1

0

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

仍然欢迎反馈,我正在回答这个问题以使其休息。

于 2010-10-13T16:49:09.840 回答