Does anyone know where Control.ShouldSeria开发者_运维百科lizeVisible is called from? The Reflector does not show it used by anything and google came up empty.
The private ShouldSerializeXxx() methods are an alternative for the [DefaultValue] attribute. That attribute cannot always be used since the default value may depend on state that can't be expressed in the DefaultValueAttribute constructor or requires a complex expression.
The method is executed through Reflection, it is exposed through the PropertyDescriptor.ShouldSerializeValue() method. Important clients of this plumbing are the PropertyGrid control (uses it to select a bold font) and the Windows Forms design-time code serializer (uses it to suppress unnecessary code).
Serialization uses reflection to access properties, that would explain why it doesn't show any usage from Reflector.
This property tells the serialization process that the Visible property has changed from its default value, and should be serialized.
This is a convention-based pattern used by designer and serializer implementations. For example, at the ComponentModel
:
PropertyDescriptor prop = TypeDescriptor.GetProperties(obj)["Visible"];
if (prop.ShouldSerializeValue(obj))
{ // write it...
}
will call that method if it exists. In the same way, this ShouldSerializeValue
abstraction is also what makes some properties appear bold in PropertyGrid
(note: it also looks at things like [DefaultValue]
).
(PropertyDescriptor
is the original way to talk about arbitrary properties in terms of binding and designers)
Equally, some serializers (certainly XmlSerializer
, but also DataContractSerializer
I believe - and protobuf-net) will respect this pattern, asking the object if it wants that property to be serialized.
精彩评论