// ========================================================
namespace Kerosene.Tools.Test
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
// ====================================================
public class Test_DeepObject
{
public static DeepObject Index_And_Dynamic_Syntax(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
DeepObject obj = new DeepObject(); Console.WriteLine("\n> Object = {0}", obj);
dynamic d = obj;
d["Country"] = "UK"; Console.WriteLine("\n> Object = {0}", obj);
d["Owner", "FirstName"] = "James"; Console.WriteLine("\n> Object = {0}", obj);
d["Owner", "LastName"] = "Bond";
d["Wierd Name for a Property"] = new CalendarDate(DateTime.Now);
d.Address.Street[0] = "Big Avenue, 1";
d.Address.Street[1] = "Secret Building";
Console.WriteLine("\n> Object = {0}", obj);
return obj;
}
public static void Dispose_Twice(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
var obj = Index_And_Dynamic_Syntax(pressEnter: false);
obj.Dispose(); Console.WriteLine("\n> Disposed = {0}", obj);
obj.Dispose(); Console.WriteLine("\n> Disposed = {0}", obj);
}
public static DeepObject Complex_Members(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
dynamic d = Index_And_Dynamic_Syntax(pressEnter: false);
d.ItemType = typeof(Dictionary<ClockTime, CalendarDate>);
//d.ItemTypeGeneric = typeof(Dictionary<,>);
d.MyDict = new Dictionary<int, CalendarDate>();
d.MyDict.Add(1, new CalendarDate(2001, 1, 1));
d.MyDict.Add(2, new CalendarDate(2002, 2, 2));
d.MyDict.Add(3, new CalendarDate(2003, 3, 3));
Console.WriteLine("\n> Object = {0}", d);
return d;
}
public static void Easy_Cloning(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
var obj = Index_And_Dynamic_Syntax(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
var temp = obj.Clone(); Console.WriteLine("\n> Cloned = {0}", temp);
if (obj != temp) Console.WriteLine("\n> {0}",
(new Exception(string.Format("Source '{0}' and cloned '{1}' are not equivalent.", obj, temp)))
.ExtendedString());
}
public static void Easy_Serialization(bool binary, bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ({1} Mode) ...", MethodHelper.ThisMethod(), binary ? "Binary" : "Text");
var obj = Index_And_Dynamic_Syntax(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
var path = "c:\\temp\\data.xml";
try { SerializationHelper.Serialize(path, obj, binary); }
catch (SerializationException e)
{
if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
throw e;
}
DeepObject temp = null;
try { temp = (DeepObject)SerializationHelper.Deserialize(path, binary); Console.WriteLine("\n> Deserialized = {0}", temp); }
catch (SerializationException e)
{
if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
throw e;
}
if (obj != temp) Console.WriteLine("\n> {0}",
(new Exception(string.Format("Source '{0}' and deserialized '{1}' are not equivalent.", obj, temp)))
.ExtendedString());
}
public static void Complex_Cloning(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
var obj = Complex_Members(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
var temp = obj.Clone(); Console.WriteLine("\n> Cloned = {0}", temp);
if (obj != temp) Console.WriteLine("\n> {0}",
(new Exception(string.Format("Source '{0}' and cloned '{1}' are not equivalent.", obj, temp)))
.ExtendedString());
}
public static void Complex_Serialization(bool binary, bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ({1} Mode) ...", MethodHelper.ThisMethod(), binary ? "Binary" : "Text");
var obj = Complex_Members(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
var path = "c:\\temp\\data.xml";
try { SerializationHelper.Serialize(path, obj, binary); }
catch (SerializationException e)
{
if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
throw e;
}
DeepObject temp = null;
try { temp = (DeepObject)SerializationHelper.Deserialize(path, binary); Console.WriteLine("\n> Deserialized = {0}", temp); }
catch (SerializationException e)
{
if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
throw e;
}
if (obj != temp) Console.WriteLine("\n> {0}",
(new Exception(string.Format("Source '{0}' and deserialized '{1}' are not equivalent.", obj, temp)))
.ExtendedString());
}
public static void Execute(bool pressEnter = true)
{
ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());
Index_And_Dynamic_Syntax(pressEnter);
Dispose_Twice(pressEnter);
Complex_Members(pressEnter);
Easy_Cloning(pressEnter);
Easy_Serialization(true, pressEnter); Easy_Serialization(false, pressEnter);
Complex_Cloning(pressEnter);
Complex_Serialization(true, pressEnter); Complex_Serialization(false, pressEnter);
}
}
}
// ========================================================