Overriding inheritance by internal attributes in C #

After dealing with a bunch of uncached exceptions when trying to serialize my classes and subclasses, I finally realized what my problem was: [Serializable] is not inherited by subclasses when applied to a base class. I am still unclear regarding the attributes of C # in general, but I understand that when creating a custom attribute, the programmer can enable the automatic inheritance of the Attribute.

Is there a way to override [Serializable] inheritance? Is there a good reason why this was not done from the very beginning and / or would it be a bad idea to do this in the first place? I would like all the subclasses of the specified base class to be serializable, so it just seems like there is no need to add an attribute to any new subclasses that I do.

Thanks!

+4
source share
1 answer

There is absolutely a good reason why this was not done in the first place - just because the base class is serializable does not mean that the derived class naturally exists.

Heck, object serializable - if serializability is inherited, it means that every class in .NET will be serializable :)

You cannot override this either - you must specify it for each class. I think this is good, in fact - when you add an attribute, you must perform mental control of the class and check whether it really makes sense to serialize it.

+5
source

All Articles