Why does adding a <Browsable(False)> attribute to a class property prevent FilterItemOnProperty() from properly retrieving the property's value?

拥有回忆 提交于 2019-12-12 01:57:15

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15442682/why-does-adding-a-browsablefalse-attribute-to-a-class-property-prevent-filte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!