Overriding inheritance on intrinsic attributes in C#
After wrestling with a bunch of uncaught exceptions when trying to serialize my classes and subclasses, I've finally understood what my problem had been: [Serializable] isn't inherited by subclasses when applied to a base class. I'm still really fuzzy about C# Attributes in general, but I do understand that when creating a custom Attribute, the programmer is able to enable automatic inheritance of the Attribute.
It there any way to override the inheritance of [Serializable]? Is there any good reason why it wasn't done from the start and/or would be a bad idea to do this in the first place? I would want all subclasses of said base class be serializable, so it just seems inelegant to have to add the attribute to any new subcl开发者_运维技巧asses I make.
Thanks!
There's absolutely a good reason it wasn't done in the first place - just because a base class is serializable doesn't mean that a derived class naturally is.
Heck, object
is serializable - if serializability were inherited, that would mean every class in .NET would be serializable :)
You can't "override" that either - you have to specify it on every class. I think that's a good thing, actually - when you add the attribute, you should perform a mental check on the class and check that it really does make sense to serialize it.
精彩评论