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:09]
admin [Attributes]
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:
-<​code ​lang="csharp">+<code csharp>
 string serializationResult = serializer.Serialize(layer);​ string serializationResult = serializer.Serialize(layer);​
 </​code>​ </​code>​
  
 Or from a string: Or from a string:
-<​code ​lang="csharp">+<code csharp>
 Layer deserializedLayer = (Layer)serializer.Deserialize(serializationResult);​ Layer deserializedLayer = (Layer)serializer.Deserialize(serializationResult);​
 </​code>​ </​code>​
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:
  
-<​code ​lang="csharp">+<code csharp>
 [Serializable] [Serializable]
 public class Test public class Test
Line 67: Line 70:
 Example: Example:
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[OnSerializing]]+[OnSerializing]
 private void SetValuesOnSerializing(StreamingContext context) private void SetValuesOnSerializing(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
 </​code>​ </​code>​
Line 79: Line 82:
 Example: Example:
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[OnSerialized]]+[OnSerialized]
 private void SetValuesOnSerialized(StreamingContext context) private void SetValuesOnSerialized(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
 </​code>​ </​code>​
Line 91: Line 94:
 Example: ​ Example: ​
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[OnDeserializing]]+[OnDeserializing]
 private void SetValuesOnDeserializing(StreamingContext context) private void SetValuesOnDeserializing(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
 </​code>​ </​code>​
Line 103: Line 106:
 Example: ​ Example: ​
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[OnDeserialized]]+[OnDeserialized]
 private void SetValuesOnDeserialized(StreamingContext context) private void SetValuesOnDeserialized(StreamingContext context)
 { {
-    ​<​nowiki>​//</​nowiki> ​……+    // ……
 } }
 </​code>​ </​code>​
Line 114: Line 117:
 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:
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[Serializable]]+[Serializable]
 public class MyControl : Control public class MyControl : Control
 { {
Line 127: Line 130:
 </​code>​ </​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":​
  
-<​code ​lang="csharp"+<code csharp>​ 
-[[NonSerializableBaseType]]+[NonSerializableBaseType]
 [Serializable] [Serializable]
 public class MyControl : Control public class MyControl : Control
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.
-<​code ​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 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.1441004947.txt.gz · Last modified: 2015/08/31 07:09 by admin