|
This Blog Has Been Moved !This Blog Has been moved to http://aleemkhan.wordpress.com Serialization is used to persist the state of the object, so the object can be regenerated with the same state later. .NET provides different types of serializations for objects,
You can use these objects for serialization. The Framework also needs to serialize the objects at various places for example in case of web services, the objects needs to be serialized for communication, in case of Remoting and also if the object is to be passed across the boundary of an Application Domain or the process. In all these cases, the framework will only be able to successfully serialize your objects if your objects are serializable. All the basic .NET types are serializable except Object, there are various other base classes which are by default serializable and can be successfully used in above scenarios, this includes the very commnly used DataSet, DataTable and other class, the complete list is These classes are serializable because they implement the ISerializable Interface. To set your own types Serializable, the most common way is to set the class/type with the [System.Serializable()] attribute, also use the [System.NonSerialized()] for the members that you don’t want to serialize. See the following example [System.Serializable] class TestClass { private string str = "Check"; [System.NonSerialized()] private SqlCommand cmd = new SqlCommand(); private int count; public CheckFunction() { ........ ........ } } However, using the serializable attribute is not the only way, and in some cases is not enough, specially if you are serializing complex objects which are inherited from other base objects. To serialize such objects you can have better control over serialization by implementing the ISerializable interface. This interface has only one method, which is GetObjectData(), this method is called when the object is to be serialized and you need to provide an extra constructor in the class to provide the de-serialization mechanism to regenerate the object. While serialization in GetObjectData() method, you can add your values to SerializationInfo parameter. During de-serialization you can get the values from the SerializationInfo object and set the values to the members of the class to regenarte the object in the same state. The example below will give you can idea class TestClass : ISerializable { private string str = "Check"; private int count; public TestClass(SerializationInfo info, StreamingContext context) { this.str = info.GetString("str"); this.count = info.GetInt32("count"); } public void GetObjectData(SerializationInfo info, StreamingContext context) { // TODO: Add TestClass.GetObjectData implementation info.AddValue("str",str); info.AddValue("count",count); } } Generally, as the MS guidelines and PAG documents say, serialization is not recommended for your custom objects, it has various issues regarding performance, security and scalability. But sometimes you have no other option then to use serialization. PAG guidelines do not recommend serialization and due to valid reasons but do not tell any other way you could get around the typical problems without serialization. Comments
8 comments have been posted.
Posted @ 6:26 PM
Hi Aleem,
My comment is not related to .NET serialization. But I like you to see this like and enjoy. http://www.thebestpageintheuniverse.net/c.cgi?u=banish Take Care
Posted @ 6:32 PM
Aaaj kal kya ho raha hai KHAN sahaab? Taraqiaan kahaan tak pohanchieen? No new post here from you recently? buzy?
Posted @ 6:24 AM
2015-12-29keyun
michael kors outlet louis vuitton bags north face fake oakley sunglasses cheap oakley sunglasses michael kors outlet online ugg boots coach factory outlet louis vuitton outlet stores abercrombie michael kors outlet online coach outlet online michael kors outlet tory burch outlet online ed hardy outlet ugg boots louis vuitton purses retro 11 p90x toms outlet michael kors outlet ray ban outlet fitflop clearance the north face jackets michael kors outlet online sale canada goose sale nike free 5.0 air max 95 michael kors outlet canada goose coats ugg boots gucci outlet uggs outlet uggs australia nfl jerseys oakley outlet jordan concords coach outlet store online clearance giuseppe zanotti outlet kids lebron shoes
Posted @ 6:14 AM
chenlina20170920
christian louboutin shoes coach outlet cheap oakley sunglasses uggs outlet mcm outlet nike nfl jerseys nike roshe ugg sale prada handbags ugg outlet store
Posted @ 8:19 PM
20170930 leilei3915
coach factory outlet canada goose jackets ugg outlet online nike factory outlet canada goose outlet canada goose jackets cheap jordans mulberry handbags prada outlet stores cheap nfl jerseys |