Does adding [Serializable] to the class have any performance implications?

为君一笑 提交于 2019-11-30 07:46:36

问题


I need to add the [Serializable] attribute to a class that is extremely performance sensitive.

Will this attribute have any performance implications on the operation of the class?


回答1:


Instances of attribute classes are only created when they're first accessed. If you don't do any serialization on that particular class, the SerializableAttribute() constructor will never be called, hence it won't cause any performance issues.

Here's an interesting article about attribute constructors: http://www.codingonthetrain.com/2008/10/attribute-constructors.html




回答2:


Attributes are a metadata annotations so they don't add weight to a class at runtime, unless they are interpreted by the runtime in a certain way that makes it treat the class differently.

[Serializable] is simply a marker attribute used as a convention to indicate that the class is serializable, it has no effect and the runtime does not treat the classes in any special way.



来源:https://stackoverflow.com/questions/1402903/does-adding-serializable-to-the-class-have-any-performance-implications

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