为什么添加< Browsable(False)>属性阻止FilterItemOnProperty()正确检索属性的值? [英] Why does adding a <Browsable(False)> attribute to a class property prevent FilterItemOnProperty() from properly retrieving the property's value?

查看:374
本文介绍了为什么添加< Browsable(False)>属性阻止FilterItemOnProperty()正确检索属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义的ComboBox控件继承自System.Windows.Forms.ComboBox。它有一个Value属性,我写如下:

 公共财产价值Get 
如果SelectedIndex = -1则返回Nothing
如果String.IsNullOrWhitespace(ValueMember)then
返回项(SelectedIndex)
返回FilterItemOnProperty(Items(SelectedIndex) ,ValueMember)
结束如果
结束Get
设置(ByVal newvalue As Object)
'...
结束集
结束属性

基本上,如果没有选择,读取值将返回 Nothing ,如果未设置ValueMember,则选择对象本身,如果设置了ValueMember,则选择对象的相应属性值。



到此为止,代码已经工作



但是,最近对用于填充ComboBox的类之一进行了更改,如下所示:

 部分公共类模态
< Browsable(False)>
Public property ModalityID As Integer
公共属性ModalityName As String
公共属性ModalityAbbrevName As String
结束类

ModalityID属性,用作ComboBox的ValueMember,已添加了Browsable(False)属性,因此在Modality集合中不会显示为列对象绑定到应用程序中其他位置的DataGridView控件。



现在打破了应用程序,因为MyComboBox1.Value现在返回所选的Modality对象,而不是ModalityID即使ComboBox的ValueMember仍然设置为ModalityID,选择的Modality对象。



挖掘.NET Framework的 ListControl的源代码。 FilterItemOnProperty(对象项,字符串字段)不是启发我。



问题是,为什么添加 ; c>

$ b $

$

$ 从方法ListControl.FilterItemOnProperty方法(对象,字符串)的参数,您可以看到属性名称以字符串(从ValueMemeber值给定)。



而且,我没有使用Reflector,而是认为方法内部创建了一个对象的属性列表,并试图找到给定的PropertyName作为字符串。



显然,根据BrowsableAttribute创建列表。



因此,如果这个< Browsable(False)>

,那么如果属性设置为False属性不会列出并且不能被找到。 code>属性只在一个类中,那么在你读取 SelectedValue 的代码中,只需读取 MobalityID



如果你用< Browsable(False)> code> ValueMemeber ComboBox 你需要考虑使用的属性的名称,也许你可以命名所有的只是ID例。



如果可能创建一个只有一个属性.ID并将其实现到所有Browsable(False)类的Inerface,然后为具有此接口的类创建一般的Value属性。然后你可以直接从代码中获取ID属性。


I've written a custom ComboBox control that inherits from System.Windows.Forms.ComboBox. It has a "Value" property that I've written as follows:

Public Property Value() As Object
    Get
        If SelectedIndex = -1 Then Return Nothing
        If String.IsNullOrWhitespace(ValueMember) Then
            Return Items(SelectedIndex)
        Else
            Return FilterItemOnProperty(Items(SelectedIndex), ValueMember)
        End If
    End Get
    Set(ByVal newvalue As Object)
        '...
    End Set
End Property

Basically, reading the Value will return Nothing if nothing is selected, the selected object itself if ValueMember is not set, or the appropriate property value of the selected object if the ValueMember is set.

Up to this point, the code has worked just fine.

However, a recent change has been made to one of the classes used to populate the ComboBox as follows:

Partial Public Class Modality
    <Browsable(False)>
    Public Property ModalityID As Integer
    Public Property ModalityName As String
    Public Property ModalityAbbrevName As String
End Class

The ModalityID property, which is used as the ValueMember of the ComboBox, has had the Browsable(False) attribute added, so it does not show up as a column when a collection of Modality objects gets bound to a DataGridView control elsewhere in the application.

This now breaks the application, because MyComboBox1.Value is now returning the selected Modality object, and not the ModalityID of the selected Modality object, even though the ComboBox's ValueMember is still set to "ModalityID".

Digging into the .NET Framework's source code for ListControl.FilterItemOnProperty(object item, string field) is not enlightening me.

The question is, why does adding a <Browsable(False)> attribute to a class property prevent FilterItemOnProperty() from properly retrieving the property's value?

解决方案

From parameters of the method ListControl.FilterItemOnProperty Method (Object, String), you can see that property name goes there as string(given from ValueMemeber value).

And, I am not used Reflector, but think that inside of method was created a list of object's properties and trying to find given PropertyName as string.

Obviously creating a list independing on BrowsableAttribute. And if Attribute set to False property not going to list and cannot be finded.

So if this <Browsable(False)> property only in one class then, in code where you reading SelectedValue, just read a MobalityID from selected object.

If you have multiply classes with <Browsable(False)> properties which used as ValueMemeber in ComboBox you need to think about names for used properties, maybe you can named all of them just "ID" for example.

If it possible create a Inerface which only have one property .ID and implement it to all your Browsable(False) classes, then create generic Value property for classes with this Interface. Then you can get a property of ID straight from code.

这篇关于为什么添加&lt; Browsable(False)&gt;属性阻止FilterItemOnProperty()正确检索属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆