VB.NET动态CustomTypeDescriptor

时间:2010-10-01 20:39:23

标签: vb.net typedescriptor icustomtypedescriptor

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

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

每次调用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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题