forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCollection.cs
More file actions
93 lines (87 loc) · 2.76 KB
/
CustomCollection.cs
File metadata and controls
93 lines (87 loc) · 2.76 KB
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
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace ServiceStack.Text.Tests.DynamicModels.DataModel
{
#if !NETCORE
[Serializable]
#endif
public class CustomCollection : Collection<CustomCollectionItem>
{
public int FindItemIndex(string name)
{
return IndexOf((from item in this
where item.Name == name
select item).FirstOrDefault());
}
public void RemoveAll(string name)
{
while (true)
{
var idx = FindItemIndex(name);
if (idx < 0)
break;
RemoveAt(idx);
}
}
public Uri AddressUri
{
get
{
var idx = FindItemIndex("AddressUri");
//Cater for value containing a real value or a serialized string value
//Using 'FromCsvField()' because 'Value' may have escaped chars
return idx < 0 ? null :
(
this[idx].Value is string
? new Uri(((string)this[idx].Value).FromCsvField())
: this[idx].Value as Uri
);
}
set
{
RemoveAll("AddressUri");
Add(new CustomCollectionItem("AddressUri", value));
}
}
public Type SomeType
{
get
{
var idx = FindItemIndex("SomeType");
//Cater for value containing a real value or a serialized string value
//Using 'FromCsvField()' because 'Value' may have escaped chars
return idx < 0 ? null :
(
this[idx].Value is string
? AssemblyUtils.FindType(((string)this[idx].Value).FromCsvField())
: this[idx].Value as Type
);
}
set
{
RemoveAll("SomeType");
Add(new CustomCollectionItem("SomeType", value));
}
}
public int IntValue
{
get
{
var idx = FindItemIndex("IntValue");
//Cater for value containing a real value or a serialized string value
return idx < 0 ? -1 :
(
this[idx].Value is string
? int.Parse((string)this[idx].Value)
: (int)this[idx].Value
);
}
set
{
RemoveAll("IntValue");
Add(new CustomCollectionItem("IntValue", value));
}
}
}
}