首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB.NET动态CustomTypeDescriptor

VB.NET动态CustomTypeDescriptor
EN

Stack Overflow用户
提问于 2010-10-02 04:39:23
回答 1查看 926关注 0票数 0

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

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

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

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

你们觉得怎么样?下面是我的提供者的示例代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-14 00:49:10

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

仍然欢迎反馈,我回答这个问题是为了让它平息下来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3842899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档