FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
protobuf-net/Examples/NetObjectOptions.cs at master · PilotMartin/protobuf-net · GitHub
PilotMartin
/
protobuf-net
Public
forked from
protobuf-net/protobuf-net
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
protobuf-net
/
Examples
/
NetObjectOptions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
240 lines (201 loc) · 9.16 KB
Breadcrumbs
protobuf-net
/
Examples
/
NetObjectOptions.cs
Copy path
File metadata and controls
240 lines (201 loc) · 9.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Text
;
using
NUnit
.
Framework
;
using
ProtoBuf
;
using
ProtoBuf
.
Meta
;
namespace
Examples
{
[
TestFixture
]
public
class
NetObjectOptions
{
[
ProtoContract
]
public
class
BasicReferenceTestOuter
{
[
ProtoMember
(
1
,
AsReference
=
true
)
]
public
BasicReferenceTestInner
Foo
{
get
;
set
;
}
[
ProtoMember
(
2
,
AsReference
=
true
)
]
public
BasicReferenceTestInner
Bar
{
get
;
set
;
}
}
[
ProtoContract
]
public
class
BasicReferenceTestInner
{
[
ProtoMember
(
1
,
AsReference
=
true
)
]
public
BasicReferenceTestInner
Self
{
get
;
set
;
}
}
[
Test
]
public
void
BasicReferenceTest
(
)
{
var
outer
=
new
BasicReferenceTestOuter
(
)
;
var
inner
=
new
BasicReferenceTestInner
(
)
;
outer
.
Foo
=
outer
.
Bar
=
inner
;
var
model
=
TypeModel
.
Create
(
)
;
model
.
Add
(
typeof
(
BasicReferenceTestOuter
)
,
true
)
;
model
.
Add
(
typeof
(
BasicReferenceTestInner
)
,
true
)
;
Assert
.
IsNotNull
(
outer
.
Foo
,
"not null before"
)
;
Assert
.
AreSame
(
outer
.
Foo
,
outer
.
Bar
,
"same before"
)
;
var
clone
=
(
BasicReferenceTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (runtime)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (runtime)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (runtime)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Bar
,
"same after (runtime)"
)
;
model
.
CompileInPlace
(
)
;
clone
=
(
BasicReferenceTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (compile in place)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (compile in place)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (compile in place)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Bar
,
"same after (compile in place)"
)
;
clone
=
(
BasicReferenceTestOuter
)
model
.
Compile
(
)
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (full compile)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (full compile)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (full compile)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Bar
,
"same after (full compile)"
)
;
}
[
Test
]
public
void
RecursiveReferenceTest
(
)
{
var
outer
=
new
BasicReferenceTestOuter
(
)
;
var
inner
=
new
BasicReferenceTestInner
(
)
;
inner
.
Self
=
inner
;
outer
.
Foo
=
inner
;
var
model
=
TypeModel
.
Create
(
)
;
model
.
Add
(
typeof
(
BasicReferenceTestOuter
)
,
true
)
;
model
.
Add
(
typeof
(
BasicReferenceTestInner
)
,
true
)
;
Assert
.
IsNotNull
(
outer
.
Foo
,
"not null before"
)
;
Assert
.
AreSame
(
outer
.
Foo
,
outer
.
Foo
.
Self
,
"same before"
)
;
var
clone
=
(
BasicReferenceTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (runtime)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (runtime)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (runtime)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Foo
.
Self
,
"same after (runtime)"
)
;
model
.
CompileInPlace
(
)
;
clone
=
(
BasicReferenceTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (compile in place)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (compile in place)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (compile in place)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Foo
.
Self
,
"same after (compile in place)"
)
;
clone
=
(
BasicReferenceTestOuter
)
model
.
Compile
(
)
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (full compile)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (full compile)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (full compile)"
)
;
Assert
.
AreSame
(
clone
.
Foo
,
clone
.
Foo
.
Self
,
"same after (full compile)"
)
;
}
[
ProtoContract
]
class
StringDynamicType
{
[
ProtoMember
(
1
,
DynamicType
=
true
)
]
public
object
Data
{
get
;
set
;
}
}
[
Test
]
public
void
StringAsDynamic
(
)
{
var
obj
=
new
StringDynamicType
{
Data
=
GetString
(
)
}
;
var
clone
=
Serializer
.
DeepClone
(
obj
)
;
Assert
.
AreEqual
(
GetString
(
)
,
clone
.
Data
)
;
}
[
Test
]
public
void
StringInterned
(
)
{
var
obj
=
new
StringInternedType
{
Foo
=
GetString
(
)
,
Bar
=
GetString
(
)
}
;
Assert
.
IsFalse
(
ReferenceEquals
(
obj
.
Foo
,
obj
.
Bar
)
)
;
var
clone
=
Serializer
.
DeepClone
(
obj
)
;
Assert
.
AreEqual
(
obj
.
Foo
,
clone
.
Foo
)
;
Assert
.
AreEqual
(
obj
.
Bar
,
clone
.
Bar
)
;
Assert
.
IsTrue
(
ReferenceEquals
(
clone
.
Foo
,
clone
.
Bar
)
)
;
}
[
Test
]
public
void
StringAsReference
(
)
{
var
obj
=
new
StringRefType
{
Foo
=
GetString
(
)
,
Bar
=
GetString
(
)
}
;
Assert
.
IsFalse
(
ReferenceEquals
(
obj
.
Foo
,
obj
.
Bar
)
)
;
var
clone
=
Serializer
.
DeepClone
(
obj
)
;
Assert
.
AreEqual
(
obj
.
Foo
,
clone
.
Foo
)
;
Assert
.
AreEqual
(
obj
.
Bar
,
clone
.
Bar
)
;
Assert
.
IsTrue
(
ReferenceEquals
(
clone
.
Foo
,
clone
.
Bar
)
)
;
}
static
string
GetString
(
)
{
return
new
string
(
'a'
,
5
)
;
}
[
ProtoContract
]
public
class
StringRefType
{
[
ProtoMember
(
1
,
DynamicType
=
true
,
AsReference
=
true
)
]
public
object
Foo
{
get
;
set
;
}
[
ProtoMember
(
2
,
DynamicType
=
true
,
AsReference
=
true
)
]
public
object
Bar
{
get
;
set
;
}
}
[
ProtoContract
]
public
class
StringInternedType
{
[
ProtoMember
(
1
)
]
public
string
Foo
{
get
;
set
;
}
[
ProtoMember
(
2
)
]
public
string
Bar
{
get
;
set
;
}
}
[
ProtoContract
]
public
class
BasicDynamicTestOuter
{
[
ProtoMember
(
1
,
DynamicType
=
true
)
]
public
object
Foo
{
get
;
set
;
}
}
[
ProtoContract
]
public
class
BasicDynamicTestInner
{
}
[
Test
]
public
void
BasicDynamicTest
(
)
{
var
outer
=
new
BasicDynamicTestOuter
(
)
;
var
inner
=
new
BasicDynamicTestInner
(
)
;
outer
.
Foo
=
inner
;
var
model
=
TypeModel
.
Create
(
)
;
model
.
Add
(
typeof
(
BasicDynamicTestOuter
)
,
true
)
;
model
.
Add
(
typeof
(
BasicDynamicTestInner
)
,
true
)
;
// assume we can at least know candidates at runtime, for now
Assert
.
IsNotNull
(
outer
.
Foo
,
"not null before"
)
;
Assert
.
IsInstanceOfType
(
typeof
(
BasicDynamicTestInner
)
,
outer
.
Foo
,
"typed before"
)
;
var
clone
=
(
BasicDynamicTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (runtime)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (runtime)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (runtime)"
)
;
Assert
.
IsInstanceOfType
(
typeof
(
BasicDynamicTestInner
)
,
outer
.
Foo
,
"typed after (runtime)"
)
;
model
.
CompileInPlace
(
)
;
clone
=
(
BasicDynamicTestOuter
)
model
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (compile in place)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (compile in place)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (compile in place)"
)
;
Assert
.
IsInstanceOfType
(
typeof
(
BasicDynamicTestInner
)
,
outer
.
Foo
,
"typed after (compile in place)"
)
;
clone
=
(
BasicDynamicTestOuter
)
model
.
Compile
(
)
.
DeepClone
(
outer
)
;
Assert
.
IsNotNull
(
clone
,
"clone exists (full compile)"
)
;
Assert
.
AreNotSame
(
outer
,
clone
,
"clone is different (full compile)"
)
;
Assert
.
IsNotNull
(
clone
.
Foo
,
"not null after (full compile)"
)
;
Assert
.
IsInstanceOfType
(
typeof
(
BasicDynamicTestInner
)
,
outer
.
Foo
,
"typed after (full compile)"
)
;
}
[
ProtoContract
]
abstract
class
BaseType
{
[
ProtoMember
(
1
)
]
public
string
Foo
{
get
;
set
;
}
}
[
ProtoContract
]
class
Derived
:
BaseType
{
[
ProtoMember
(
1
)
]
public
int
Bar
{
get
;
set
;
}
}
[
ProtoContract
]
class
Wrapper
{
[
ProtoMember
(
1
,
DynamicType
=
true
)
]
public
object
Value
{
get
;
set
;
}
}
[
Test
]
// this is failing currently; needs to handle base-type via dynamictype
public
void
TestUnknownDerivedType
(
)
{
var
obj
=
new
Wrapper
{
Value
=
new
Derived
{
Bar
=
123
,
Foo
=
"abc"
}
}
;
var
clone
=
Serializer
.
DeepClone
(
obj
)
;
Assert
.
IsInstanceOfType
(
typeof
(
Derived
)
,
clone
.
Value
)
;
Derived
d
=
(
Derived
)
clone
.
Value
;
Assert
.
AreEqual
(
123
,
d
.
Bar
)
;
Assert
.
AreEqual
(
"abc"
,
d
.
Foo
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL