//
// Microsoft Developer & Platform Evangelism
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// Copyright (c) Microsoft Corporation. All Rights Reserved.
// This code is released under the terms of the MS-LPL license,
// http://microsoftnlayerapp.codeplex.com/license
using System;
using System.IO;
using System.Runtime.Serialization;
namespace Presentation.Web.MVC.Client.Extensions.Utilities
{
///
/// This class serializes and deserializes an entity to a base 64 string.
///
/// Type of the entity to be serialized or deserialized.
public class SelfTrackingEntityBase64Converter
{
#region Members
///
/// Deserializes the entity from a base 64 string.
///
/// The base 64 string containing the serialized entity.
/// The resulting entity after deserialization.
public T ToEntity(string value)
{
var bytes = Convert.FromBase64String(value);
MemoryStream memStream = new MemoryStream(bytes);
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
T result = (T)deserializer.ReadObject(memStream);
return result;
}
///
/// Serialized the entity to a base 64 string.
///
/// The entity to be serialized.
/// The base 64 string containing the serialized entity.
public string ToBase64(T entity)
{
MemoryStream memStream = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(memStream, entity);
string result = Convert.ToBase64String(memStream.ToArray());
return result;
}
#endregion
}
}