Click or drag to resize

CTContractSerializationT Class

Serialization class for CADSharpTools. Use this class for objects that contain properties with private setters.
Inheritance Hierarchy
SystemObject
  CADSharpTools.SerializationCTContractSerializationT

Namespace:  CADSharpTools.Serialization
Assembly:  CADSharpTools.PDM (in CADSharpTools.PDM.dll) Version: 1.4.16.0 (1.4.16)
Syntax
public class CTContractSerialization<T>
where T : class

Type Parameters

T
Type

The CTContractSerializationT type exposes the following members.

Constructors
Methods
  NameDescription
Public methodStatic memberCode exampleDeserialize
De-serializes an XML string into the specified type.
Public methodCode exampleSaveToXML
Saves an object of a specified type to an XML file.
Public methodCode exampleSerialize
Serializes a type into an XML string.
Top
Remarks
The specified type must have its properties that contain a private setter decorated with DataMemberAttribute.
Examples
This example shows how to use the CTContractSerialization class.
public class Person
{
   [DataMember]
   public string FirstName { get; private set; }
   [DataMember]
   public string LastName { get; private set; }
   [DataMember]
   public uint Age { get; private set; }
   private Person()
   {
   }
   public static Person CreateInstance()
   {
      var p = new Person;
      p.LastName = "Jlili";
      p.FirstName = "Amen";
      p.Age = 29;
      return p;
   }
   public override string ToString()
   {
       return string.Format("{0} {1}", this.FirstName, this.LastName);
   }
}
public class Program
{
   static void Main(string[] args)
   {
       // Create new instance of the CTContractSerialization
       var cTSerializer = new CTContractSerialization<Person >();
       // Create a new person 
       var Person = Person.CreateInstance();
       // Serialize the person 
       var serializedPersonReturn = cTSerializer.Serialize(Person);
       if (serializedPersonReturn.IsError)
       {
           System.Diagnostics.Debug.Print(string.Format("Exception caught. Message = {0}", serializedPersonReturn.Exception.Message));
           Console.ReadKey();
           return;
       }
       var serializedPerson = serializedPersonReturn.Value;
       // Print XML 
       System.Diagnostics.Debug.Print(serializedPerson);
       // Deserialize XML 
       var reserializedPersonReturn = CTContractSerialization<Person>.Deserialize(serializedPerson);
       if (reserializedPersonReturn.IsError)
       {
           System.Diagnostics.Debug.Print(string.Format("Exception caught. Message = {0}", reserializedPersonReturn.Exception.Message));
           Console.ReadKey();
           return;
       }
       var reserializedPerson = reserializedPersonReturn.Value;
       // Print full name 
       System.Diagnostics.Debug.Print(reserializedPerson.ToString());
       // Save to XML file
       string fileName = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "person.xml");
       cTSerializer.SaveToXML(reserializedPerson, fileName);
       Console.ReadKey();
   }
 }
See Also