1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
//
// System.Runtime.Serialization.FormatterServicesTests: NUnit test
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2002 Ximian Inc. (http://www.ximian.com)
//
using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.Serialization;
namespace MonoTests.System.Runtime.Serialization
{
public class FormatterServicesTests
{
public void TestClass1 ()
{
DerivedClass1 derived = new DerivedClass1 ();
derived.anotherInt = 69;
MemberInfo [] members = FormatterServices.GetSerializableMembers (derived.GetType ());
Assert.IsTrue (members != null, "#01");
Assert.AreEqual (3, members.Length, "#02");
object [] data = FormatterServices.GetObjectData (derived, members);
Assert.IsTrue (data != null, "#03");
Assert.AreEqual (3, data.Length, "#04");
DerivedClass1 o = (DerivedClass1) FormatterServices.GetUninitializedObject (derived.GetType ());
Assert.IsTrue (o != null, "#05");
o = (DerivedClass1) FormatterServices.PopulateObjectMembers (o, members, data);
Assert.IsTrue (o != null, "#06");
Assert.AreEqual ("hola", o.Hello, "#07");
Assert.AreEqual (21, o.IntBase, "#08");
Assert.AreEqual (1, o.IntDerived, "#09");
Assert.AreEqual (69, o.anotherInt, "#10");
Assert.AreEqual ("hey", DerivedClass1.hey, "#11");
}
}
[Serializable]
class BaseClass1
{
public string hello = "hola";
static int intBase = 21;
public override int GetHashCode ()
{
return base.GetHashCode ();
}
public override bool Equals (object o)
{
BaseClass1 bc = o as BaseClass1;
if (o == null)
return false;
if (hello != "hola")
return false;
return (hello == bc.hello);
}
public string Hello
{
get {
return hello;
}
}
public int IntBase
{
get {
return intBase;
}
}
}
[Serializable]
class DerivedClass1 : BaseClass1
{
private int intDerived = 1;
[NonSerialized] public int publicint = 2;
public int anotherInt = 22;
public static string hey = "hey";
public string Name
{
get {
return "Ahem";
}
}
public void SomeMethod ()
{
/* Does nothing */
}
public override int GetHashCode ()
{
return base.GetHashCode ();
}
public override bool Equals (object o)
{
DerivedClass1 dc = o as DerivedClass1;
if (o == null)
return false;
if (anotherInt != 22 || hey != "hey")
return false;
return (anotherInt == dc.anotherInt);
}
public int IntDerived
{
get {
return intDerived;
}
}
}
}
|