serialization & SerializableAttribute

  • Thread starter Thread starter Ashutosh
  • Start date Start date
A

Ashutosh

Hi,
Can someone please tell me whats so special in applying the
"Serializable" attribute to a type??? I understand that it must be
applied to a type if it has to be serialized. I want know what extra
work the compiler does in this case?

I know how to crate attributes and how a method/type can check for the
presence of a attribute and also that this is a Pseudo-custom
attribute....When any attribute is applied to a type, the compiler just
adds the information to the meta data of the module, which can be
retrieved at the run time.

*So, does the compiler does some check on the type or restrict the
declaration of a type when Serializable attribute is applied to it?*

Thanks & Regards,
Ashutosh
 
Hi Ashutosh,

As for the "SerializableAttribute", it is the same as other .NET Attributes
which decorate the certain class or class member (SerializableAttribute is
for class level). And the compiler will just emit some info in metadata of
the certain class to indicate that the class has marked with serializable.

About "how this attribute will work at runtime", here is the case when it
will perform important roles:

**Whenever you try persist a class instance into some storage which need to
ensure the class is serializable(the serialize here is binary serialize),
such as SQL Server session state, ASP.NET ViewState, , the runtime will try
inspecting the metadata of that class to see whether it has been marked
with this attribute. If so, that instance will be serialized(by the .NET
runtime's default object serializer) and stored into the target storage.
Otherwise, exception will be thrown.

**Or if you explicitly use BinarySerializer to serialize a class instance,
the runtime will also perform such check to ensure the class is
serialize(either marked with this attribute or has explicitly implement
the ISerializable interface).


Also, from the "how the binary serialization is implemented" perspective,
using "SerializableAttribute" means you let the .NET runtime help you
generate the object serializing rule and logic automatically. While if you
manually implement "ISerializable" interface, you will need to add the code
logic for serializing the certain class's instances.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
Hi Ashutosh,

Have you got any progress on this issue or does the information in my last
reply help you some?

If you still have any questions or anything else need help, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).


--------------------
From: (e-mail address removed) ("Steven Cheng")
Organization: Microsoft
Date: Thu, 16 Oct 2008 03:12:26 GMT
Subject: RE: serialization & SerializableAttribute
 
Back
Top