开发者

Change Property from a base class to [NonSerialized]

So I have a base class and there a property that is set to be [Serializable]. In the derived class I would like to make that property [N开发者_运维问答onSerialized].

How can this be done?


This would be a violation of OOP. The base class has established the fact that this property is serializable. It must be possible to substitute any instance of a derived class for an instance of the base class. This means that every instance of the derived class must have that property serializable.


You cannot substitute a new attribute, but you can define a property called ShouldSerializePropertyName (where PropertyName is the name of your property) that always returns false. This should override the presence of the attribute on the property.

For example,

public class BaseClass
{
    [Serialized]
    public string MyProperty { get; set; }
}

public class ChildClass : BaseClass
{
    public bool ShouldSerializeMyProperty { get { return false; } }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜