-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressParseResult.cs
More file actions
148 lines (127 loc) · 4.17 KB
/
AddressParseResult.cs
File metadata and controls
148 lines (127 loc) · 4.17 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
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
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
namespace EasyKeys.Shipping.PostalAddress;
/// <summary>
/// Contains the fields that were extracted by the <see cref="AddressParser"/> object.
/// </summary>
public class AddressParseResult
{
/// <summary>
/// The street line.
/// </summary>
private string? _streetLine;
/// <summary>
/// Initializes a new instance of the <see cref="AddressParseResult"/> class.
/// </summary>
/// <param name="fields">The fields that were parsed.</param>
internal AddressParseResult(Dictionary<string, string> fields)
{
if (fields == null)
{
throw new ArgumentNullException("fields");
}
var type = GetType();
foreach (var pair in fields)
{
var bindingFlags =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.IgnoreCase;
var propertyInfo = type.GetProperty(pair.Key, bindingFlags);
if (propertyInfo != null)
{
propertyInfo.SetValue(this, Convert.ChangeType(pair.Value, propertyInfo.PropertyType), null);
}
}
}
/// <summary>
/// Gets the city name.
/// </summary>
public string? City { get; private set; }
/// <summary>
/// Gets the house number.
/// </summary>
public string? Number { get; private set; }
/// <summary>
/// Gets the predirectional, such as "N" in "500 N Main St".
/// </summary>
public string? Predirectional { get; private set; }
/// <summary>
/// Gets the postdirectional, such as "NW" in "500 Main St NW".
/// </summary>
public string? Postdirectional { get; private set; }
/// <summary>
/// Gets the state or territory.
/// </summary>
public string? State { get; private set; }
/// <summary>
/// Gets the name of the street, such as "Main" in "500 N Main St".
/// </summary>
public string? Street { get; private set; }
/// <summary>
/// Gets the full street line, such as "500 N Main St" in "500 N Main St".
/// This is typically constructed by combining other elements in the parsed result.
/// However, in some special circumstances, most notably APO/FPO/DPO addresses, the
/// street line is set directly and the other elements will be null.
/// </summary>
public string StreetLine
{
get
{
if (_streetLine == null)
{
var streetLine = string.Join(
" ",
new[]
{
Number,
Predirectional,
Street,
Suffix,
Postdirectional,
SecondaryUnit,
SecondaryNumber
});
streetLine = Regex
.Replace(streetLine, @"\ +", " ")
.Trim();
return streetLine;
}
return _streetLine;
}
private set => _streetLine = value;
}
/// <summary>
/// Gets the street suffix, such as "ST" in "500 N MAIN ST".
/// </summary>
public string? Suffix { get; private set; }
/// <summary>
/// Gets the secondary unit, such as "APT" in "500 N MAIN ST APT 3".
/// </summary>
public string? SecondaryUnit { get; private set; }
/// <summary>
/// Gets the secondary unit, such as "3" in "500 N MAIN ST APT 3".
/// </summary>
public string? SecondaryNumber { get; private set; }
/// <summary>
/// Gets the ZIP code.
/// </summary>
public string? Zip { get; private set; }
/// <summary>
/// Returns a <see cref="string"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents this instance.
/// </returns>
public override string ToString()
{
return string.Format(
CultureInfo.InvariantCulture,
"{0}; {1}, {2} {3}",
StreetLine,
City,
State,
Zip);
}
}