-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lesson6Inheritance.cs
153 lines (135 loc) · 4.54 KB
/
Lesson6Inheritance.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using NUnit.Framework;
using ProtoBuf;
using ProtoBuf.Meta;
namespace ProtobufNet.Learning.Tests.Integration
{
public class Lesson6Inheritance:LessonBase
{
#region Test1 Base class knows about the super class
[Test]
public void BaseClassKnowsAboutSuperClass()
{
var instance = new Lesson6SuperClass1
{
Name ="Test1Name",
Phone = "Test1Phone"
};
var result = PassThroughProtobuf(instance);
Assert.AreEqual("Test1Name",result.Name);
Assert.AreEqual("Test1Phone",result.Phone);
}
[ProtoContract]
[ProtoInclude(100, typeof(Lesson6SuperClass1))]
public class Lesson6BaseClass1
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
public class Lesson6SuperClass1:Lesson6BaseClass1
{
[ProtoMember(1)]
public string Phone { get; set; }
}
#endregion
#region Test2 When the base class cannot know about the super class
[Test]
public void BaseClassDoesntKnow()
{
// Here you need to configure the runtime to
// work with it
ProtobufTypeModel
.Add(typeof (Lesson6BaseClass2), true)
.AddSubType(100, typeof (Lesson6SuperClass2));
var instance = new Lesson6SuperClass2
{
Name = "Test2Name",
Phone = "Test2Phone"
};
var result = PassThroughProtobuf(instance);
Assert.AreEqual("Test2Phone", result.Phone);
Assert.AreEqual("Test2Name", result.Name);
}
[ProtoContract]
public class Lesson6BaseClass2
{
[ProtoMember(2)]
public string Name { get; set; }
}
[ProtoContract]
public class Lesson6SuperClass2 : Lesson6BaseClass2
{
[ProtoMember(1)]
public string Phone { get; set; }
}
#endregion
#region Test3 Here we use the base class to ser/des and we get the superclass
[Test]
public void BaseClassDoesntKnowUsingBaseClass()
{
// Here you need to configure the runtime to
// work with it
ProtobufTypeModel
.Add(typeof(Lesson6BaseClass3), true)
.AddSubType(100, typeof(Lesson6SuperClass3));
Lesson6BaseClass3 instance = new Lesson6SuperClass3
{
Name = "Test3Name",
Phone = "Test3Phone"
};
var result = PassThroughProtobuf(instance) as Lesson6SuperClass3;
Assert.AreEqual("Test3Phone", result.Phone);
Assert.AreEqual("Test3Name", result.Name);
}
[ProtoContract]
public class Lesson6BaseClass3
{
[ProtoMember(2)]
public string Name { get; set; }
}
[ProtoContract]
public class Lesson6SuperClass3 : Lesson6BaseClass3
{
[ProtoMember(1)]
public string Phone { get; set; }
}
#endregion
#region Test4 What if we use the base class object
[Test]
public void UsingBaseClassObject()
{
var instance = new Lesson6Test4Class1
{
Something = new Lesson6Test4Class2
{
Name ="Test4Name"
}
};
var result = PassThroughProtobuf(instance);
Assert.AreEqual("Test4Name", ((Lesson6Test4Class2)result.Something).Name);
}
[Test]
public void UsingBaseClassObjectSetNull()
{
var instance = new Lesson6Test4Class1
{
Something = null
};
var result = PassThroughProtobuf(instance);
Assert.IsNull(result.Something);
}
[ProtoContract]
public class Lesson6Test4Class1
{
[ProtoMember(1,DynamicType = true)]
public object Something { get; set; }
}
[ProtoContract]
public class Lesson6Test4Class2
{
[ProtoMember(1)]
public string Name { get; set; }
}
#endregion
}
}