User Tools

Site Tools


map_suite_serialization_guide

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
map_suite_serialization_guide [2015/08/31 07:06]
admin [Using a Stream]
map_suite_serialization_guide [2019/10/21 07:07] (current)
tgwikiupdate [Map Suite Serialization Guide]
Line 1: Line 1:
 ====== Map Suite Serialization Guide ====== ====== Map Suite Serialization Guide ======
 +
 +{{section>​upgrade_map_suite_to_10.0}}
 +
 =====Serialization & Deserialization===== =====Serialization & Deserialization=====
 ====Using a File==== ====Using a File====
 For example, if we want to serialize a layer and save it to the hard disk, then we can use the GeoSerializer using the code below. For example, if we want to serialize a layer and save it to the hard disk, then we can use the GeoSerializer using the code below.
  
-<​code ​lang="csharp">+<code csharp>
 Layer layer = GetLayer(); Layer layer = GetLayer();
 GeoSerializer serializer = new GeoSerializer();​ GeoSerializer serializer = new GeoSerializer();​
Line 12: Line 15:
 The layer can be recreated from the file like this: The layer can be recreated from the file like this:
  
-<​code ​lang="csharp">+<code csharp>
 Layer deserializedLayer = (Layer)serializer.Deserialize(@"​C:​\layer.xml",​ FileAccess.Read);​ Layer deserializedLayer = (Layer)serializer.Deserialize(@"​C:​\layer.xml",​ FileAccess.Read);​
 </​code>​ </​code>​
Line 20: Line 23:
  
 To a Stream: To a Stream:
-<​code ​lang="csharp">+<code csharp>
 using (MemoryStream memoryStream = new MemoryStream()) using (MemoryStream memoryStream = new MemoryStream())
 { {
Line 28: Line 31:
  
 From a Stream: From a Stream:
-<​code ​lang="csharp">+<code csharp>
 using (FileStream fileStream = File.Create(@"​C:​\layer.xml"​)) using (FileStream fileStream = File.Create(@"​C:​\layer.xml"​))
 { {
Line 37: Line 40:
 ====Using a String==== ====Using a String====
 To a string: To a string:
-<source lang="csharp">+<code csharp>
 string serializationResult = serializer.Serialize(layer);​ string serializationResult = serializer.Serialize(layer);​
-</source>+</code>
  
 Or from a string: Or from a string:
-<source lang="csharp">+<code csharp>
 Layer deserializedLayer = (Layer)serializer.Deserialize(serializationResult);​ Layer deserializedLayer = (Layer)serializer.Deserialize(serializationResult);​
-</source>+</code>
  
 =====Serialization Requirements===== =====Serialization Requirements=====
Line 53: Line 56:
 The GeoSerializer can only serialize and deserialize types that are decorated with SerializableAttribute,​ like this: The GeoSerializer can only serialize and deserialize types that are decorated with SerializableAttribute,​ like this:
  
-<source lang="csharp"+<code csharp>​ 
- [[Serializable]+[Serializable] 
-    public class Test +public class Test 
-    +
-  <​nowiki>​//</​nowiki>​…… +    //…… 
-    +
-</source>+</code>
  
 If the type "​Test"​ has a field whose type is not decorated with the Serializable attribute, then that field should be decorated with NonSerializedAttribute,​ else the GeoSerializer will throw an exception when it tries to serialize that field'​s value. If the type "​Test"​ has a field whose type is not decorated with the Serializable attribute, then that field should be decorated with NonSerializedAttribute,​ else the GeoSerializer will throw an exception when it tries to serialize that field'​s value.
Line 67: Line 70:
 Example: Example:
  
-<source lang="csharp"+<code csharp>​ 
-[[OnSerializing]]+[OnSerializing]
 private void SetValuesOnSerializing(StreamingContext context) private void SetValuesOnSerializing(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
-</source>+</code>
  
 ===OnSerializedAttribute=== ===OnSerializedAttribute===
Line 79: Line 82:
 Example: Example:
  
-<source lang="csharp"+<code csharp>​ 
-[[OnSerialized]]+[OnSerialized]
 private void SetValuesOnSerialized(StreamingContext context) private void SetValuesOnSerialized(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
-</source>+</code>
  
 ===OnDeserializingAttribute=== ===OnDeserializingAttribute===
Line 91: Line 94:
 Example: ​ Example: ​
  
-<source lang="csharp"+<code csharp>​ 
-[[OnDeserializing]]+[OnDeserializing]
 private void SetValuesOnDeserializing(StreamingContext context) private void SetValuesOnDeserializing(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
-</source>+</code>
  
 ===OnDeserializedAttribute=== ===OnDeserializedAttribute===
Line 103: Line 106:
 Example: ​ Example: ​
  
-<source lang="csharp"+<code csharp>​ 
-[[OnDeserialized]]+[OnDeserialized]
 private void SetValuesOnDeserialized(StreamingContext context) private void SetValuesOnDeserialized(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
-</source>+</code>
  
 ===NonSerializableBaseTypeAttribute=== ===NonSerializableBaseTypeAttribute===
 The NonSerializableBaseTypeAttribute is useful when your type inherits from a type that is not serializable,​ for example: The NonSerializableBaseTypeAttribute is useful when your type inherits from a type that is not serializable,​ for example:
  
-<source lang="csharp"+<code csharp>​ 
-[[Serializable]]+[Serializable]
 public class MyControl : Control public class MyControl : Control
 { {
Line 125: Line 128:
     }     }
 } }
-</source>+</code>
  
-The type "​MyControl"​ inherits from System.Window.Control.Control. ​ Now if we try to serialize an instance of MyControl, the serializer will throw an exception because the type "​Control"​ contains fields of types that are not serializable. ​ The NonSerializedAttribute we mentioned before does not help here, because we don't own the type "​Control",​ so we can't put attributes to its fields. ​ At this point, our only solution is to put [[NonSerializableBaseType]] on "​MyControl":​+The type "​MyControl"​ inherits from System.Window.Control.Control. ​ Now if we try to serialize an instance of MyControl, the serializer will throw an exception because the type "​Control"​ contains fields of types that are not serializable. ​ The NonSerializedAttribute we mentioned before does not help here, because we don't own the type "​Control",​ so we can't put attributes to its fields. ​ At this point, our only solution is to put [NonSerializableBaseType] on "​MyControl":​
  
-<source lang="csharp"+<code csharp>​ 
-[[NonSerializableBaseType]]+[NonSerializableBaseType]
 [Serializable] [Serializable]
 public class MyControl : Control public class MyControl : Control
Line 141: Line 144:
     }     }
 } }
-</source>+</code>
  
 Now when we serialize an instance of "​MyControl",​ its base type's fields will be ignored, thus no exceptions. Now when we serialize an instance of "​MyControl",​ its base type's fields will be ignored, thus no exceptions.
Line 173: Line 176:
  
 Here is a sample of how to make your own serializer. Here is a sample of how to make your own serializer.
-<source lang="csharp">+<code csharp>
 public class MyGeoObjectModeler : GeoObjectModeler public class MyGeoObjectModeler : GeoObjectModeler
 { {
     protected override Collection<​MemberInfo>​ GetMembers(Type type, GeoObjectModelerMemberTypes memberTypes)     protected override Collection<​MemberInfo>​ GetMembers(Type type, GeoObjectModelerMemberTypes memberTypes)
     {     {
-        ​<​nowiki>​//</​nowiki>​......+        //......
     }     }
  
Line 185: Line 188:
 memberOwner,​ GeoObjectNode memberNode) memberOwner,​ GeoObjectNode memberNode)
     {     {
-        ​<​nowiki>​//</​nowiki>​......+        //......
     }     }
  
Line 192: Line 195:
 GeoObjectNode memberNode) GeoObjectNode memberNode)
     {     {
-        ​<​nowiki>​//</​nowiki>​......+        //......
     }     }
 } }
Line 219: Line 222:
     }     }
 } }
-</source>+</code>
  
 ====Events==== ====Events====
Line 248: Line 251:
 ===LoadCore=== ===LoadCore===
 Recreates a GeoObjectModel from a stream. Recreates a GeoObjectModel from a stream.
- 
-[[Category:​Map Suite Services Edition]] [[Category:​Web GIS]] [[Category:​Desktop GIS]] [[Category:​GIS]] [[Category:​.NET]] 
- 
map_suite_serialization_guide.1441004816.txt.gz · Last modified: 2015/08/31 07:06 by admin