我正在尝试一个想法(以前从未使用过TypeDescriptors ),并设法让它很好地工作。但我对我在我的小实验中做出的一些“最佳实践”决定感到担忧。
我使用了一个CustomTypeDescriptor,它从它的PropertyDescriptors接收一个事件,表明值正在改变或被查询。
每次调用GetTypeDescriptor时,TypeDescriptorProvider都会生成一个全新的CustomTypeDescriptor实例,然后将CustomTypeDescriptor上的事件绑定到instance对象。
我不确定每次调用GetTypeDescriptor时生成一个新的CustomTypeDescriptor是否是一个好主意(尽管我不得不这样做,才能让它正常工作)。我也不确定直接从CustomTypeDescriptor到instance对象的绑定事件是否有任何后果,特别是如果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发布于 2010-10-14 00:49:10
我已经用了一段时间了,它一点都没有爆炸。
仍然欢迎反馈,我回答这个问题是为了让它平息下来。
https://stackoverflow.com/questions/3842899
复制相似问题