This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
map_suite_serialization_guide [2015/08/20 03:08] 127.0.0.1 external edit |
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}} | ||
- | {{article rating}} | ||
=====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. | ||
- | <source lang="csharp"> | + | <code csharp> |
Layer layer = GetLayer(); | Layer layer = GetLayer(); | ||
GeoSerializer serializer = new GeoSerializer(); | GeoSerializer serializer = new GeoSerializer(); | ||
serializer.Serialize(layer, @"C:\layer.xml"); | serializer.Serialize(layer, @"C:\layer.xml"); | ||
- | </source> | + | </code> |
The layer can be recreated from the file like this: | The layer can be recreated from the file like this: | ||
- | <source lang="csharp"> | + | <code csharp> |
Layer deserializedLayer = (Layer)serializer.Deserialize(@"C:\layer.xml", FileAccess.Read); | Layer deserializedLayer = (Layer)serializer.Deserialize(@"C:\layer.xml", FileAccess.Read); | ||
- | </source> | + | </code> |
====Using a Stream==== | ====Using a Stream==== | ||
Line 22: | Line 23: | ||
To a Stream: | To a Stream: | ||
- | <source lang="csharp"> | + | <code csharp> |
using (MemoryStream memoryStream = new MemoryStream()) | using (MemoryStream memoryStream = new MemoryStream()) | ||
{ | { | ||
serializer.Serialize(layer, memoryStream); | serializer.Serialize(layer, memoryStream); | ||
} | } | ||
- | </source> | + | </code> |
From a Stream: | From a Stream: | ||
- | <source lang="csharp"> | + | <code csharp> |
using (FileStream fileStream = File.Create(@"C:\layer.xml")) | using (FileStream fileStream = File.Create(@"C:\layer.xml")) | ||
{ | { | ||
Layer deserializedLayer = (Layer)serializer.Deserialize(fileStream); | Layer deserializedLayer = (Layer)serializer.Deserialize(fileStream); | ||
} | } | ||
- | </source> | + | </code> |
====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 55: | 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 69: | 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 81: | 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 93: | 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 105: | 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 127: | 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 143: | 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 175: | 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 187: | Line 188: | ||
memberOwner, GeoObjectNode memberNode) | memberOwner, GeoObjectNode memberNode) | ||
{ | { | ||
- | <nowiki>//</nowiki>...... | + | //...... |
} | } | ||
Line 194: | Line 195: | ||
GeoObjectNode memberNode) | GeoObjectNode memberNode) | ||
{ | { | ||
- | <nowiki>//</nowiki>...... | + | //...... |
} | } | ||
} | } | ||
Line 221: | Line 222: | ||
} | } | ||
} | } | ||
- | </source> | + | </code> |
====Events==== | ====Events==== | ||
Line 250: | 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]] | ||
- |