- dataobjectbase_2.1.0.zip
- doc
- lib
- CatenaLogic.BlogArticles.dll
- output
- Release
- CatenaLogic.BlogArticles.dll
- CatenaLogic.Examples.exe
- Data
- Example 01 - Simple object
- Example 02 - Error validation
- Example 03 - Serialization
- Example 04 - Nested objects
- Example 05 - Backward compatibility
- Example 06 - Revert and apply
- screenshots
- Example 02 - Error validation.png
- snippets
- dataobject.snippet
- propdata.snippet
- src
- CatenaLogic.Examples
- App.xaml
- App.xaml.cs
- CatenaLogic.Examples.csproj
- Data
- Example 01 - Simple object
- Example 02 - Error validation
- Example 03 - Serialization
- Example 04 - Nested objects
- Example 05 - Backward compatibility
- Example 06 - Revert and apply
- Properties
- UI
- Input
- Windows
- Example 02 - Error validation
- Example 03 - Serialization
- Example 05 - Backward compatibility
- Example 06 - Revert and apply
- CatenaLogic.Test
- CatenaLogic
- DataObjectBase.sln
- DataObjectBase.vsmdi
- LocalTestRun.testrunconfig
- dataobjectbase.zip
|
using System;
using System.Runtime.Serialization;
using CatenaLogic.Data;
namespace CatenaLogic.Examples.Data.OldNamespace
{
/// <summary>
/// Person class in an "old" (standard .NET) style.
/// </summary>
/// <remarks>
/// All base functionality, such as the INotifyPropertyChanged, are not implemented in this class. The
/// goal of this "old" style class is to show that objects that were previously binary serialized in a custom
/// way, can easily be transformed into the DataObjectBase class.
/// </remarks>
[Serializable]
public class Person : ISerializable
{
#region Variables
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="Person"/> class.
/// </summary>
public Person()
{
// Set default values
FirstName = "Geert";
LastName = "van Horrik";
}
/// <summary>
/// Initializes a new instance of the <see cref="Person"/> class.
/// </summary>
/// <param name="info">The info.</param>
/// <param name="context">The context.</param>
/// <remarks>
/// Constructor required for serialization.
/// </remarks>
public Person(SerializationInfo info, StreamingContext context)
{
// Read all data from the info
FirstName = (string)info.GetValue("FirstName", typeof (string));
LastName = (string)info.GetValue("LastName", typeof(string));
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the first name.
/// </summary>
/// <value>The first name.</value>
public string FirstName { get; set; }
/// <summary>
/// Gets or sets the last name.
/// </summary>
/// <value>The last name.</value>
public string LastName { get; set; }
#endregion
#region Methods
#endregion
#region ISerializable Members
/// <summary>
/// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
/// <exception cref="T:System.Security.SecurityException">
/// The caller does not have the required permission.
/// </exception>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Write object data to info
info.AddValue("FirstName", FirstName);
info.AddValue("LastName", LastName);
}
#endregion
}
}
namespace CatenaLogic.Examples.Data.NewNamespace
{
/// <summary>
/// Person Data object class which fully supports serialization, property changed notifications,
/// backwards compatibility and error checking.
/// </summary>
/// <remarks>
/// As you can see, the RedirectType attribute is used to redirect an old type to a complete new
/// assembly, namespace or type name.
/// </remarks>
[Serializable]
[RedirectType("CatenaLogic.Examples", "CatenaLogic.Examples.Data.OldNamespace.Person")]
public class Person : DataObjectBase<Person>
{
#region Constructor & destructor
/// <summary>
/// Initializes a new object from scratch.
/// </summary>
public Person()
: base() { }
/// <summary>
/// Initializes a new object based on <see cref="SerializationInfo"/>.
/// </summary>
/// <param name="info"><see cref="SerializationInfo"/> that contains the information.</param>
/// <param name="context"><see cref="StreamingContext"/>.</param>
public Person(SerializationInfo info, StreamingContext context)
: base(info, context) { }
#endregion
#region Properties
/// <summary>
/// Gets or sets the first name.
/// </summary>
public string FirstName
{
get { return GetValue<string>(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
/// <summary>
/// Register the property so it is known in the class.
/// </summary>
public readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string), string.Empty);
/// <summary>
/// Gets or sets the last name.
/// </summary>
public string LastName
{
get { return GetValue<string>(LastNameProperty); }
set { SetValue(LastNameProperty, value); }
}
/// <summary>
/// Register the property so it is known in the class.
/// </summary>
public readonly PropertyData LastNameProperty = RegisterProperty("LastName", typeof(string), string.Empty);
#endregion
#region Serialization
/// <summary>
/// Retrieves the actual data from the serialization info.
/// </summary>
/// <param name="info"><see cref="SerializationInfo"/>.</param>
/// <remarks>
/// This method should only be implemented if backwards compatibility should be implemented for
/// a class that did not previously implement the DataObjectBase class.
/// </remarks>
protected override void GetDataFromSerializationInfo(SerializationInfo info)
{
// Check if deserialization succeeded
if (DeserializationSucceeded) return;
// Deserialization did not succeed for any reason, so retrieve the values manually
// Luckily there is a helper class (SerializationHelper) that eases the deserialization of "old" style objects
FirstName = SerializationHelper.GetString(info, "FirstName", FirstNameProperty.GetDefaultValue<string>());
LastName = SerializationHelper.GetString(info, "LastName", LastNameProperty.GetDefaultValue<string>());
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.