My favorites
|
Sign in
octalforty-brushie
A set of general-purpose .NET libraries and classes
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
‹r158
r171
Source path:
svn
/
trunk
/
octalforty.Brushie.Web
/
QueryStringCompiler.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
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections;
#if FW2
using System.Collections.Generic;
#endif
using System.Reflection;
namespace octalforty.Brushie.Web
{
/// <summary>
/// Compiles a query string from an object with properties marked with <see cref="QueryStringFieldAttribute"/>.
/// </summary>
public class QueryStringCompiler
{
/// <summary>
/// Initializes a new instance of <see cref="QueryStringCompiler"/> class.
/// </summary>
public QueryStringCompiler()
{
}
/// <summary>
/// Compiles a query string from the <paramref name="container"/>.
/// </summary>
/// <param name="container"></param>
/// <returns></returns>
public string Compile(object container)
{
#if FW2
List<string> fields = new List<string>();
#else
ArrayList fields = new ArrayList();
#endif
PropertyInfo[] properties =
container.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
CompileProperties(container, properties, fields);
#if FW2
return string.Join("&", fields.ToArray());
#else
return string.Join("&", (string[])fields.ToArray(typeof(string)));
#endif
}
private void CompileProperties(object container, PropertyInfo[] properties,
#if FW2
List<string> fields
#else
ArrayList fields
#endif
)
{
foreach(PropertyInfo property in properties)
{
if(Attribute.IsDefined(property, typeof(QueryStringFieldAttribute)))
{
CompileProperty(container, property, fields);
} // if
} // foreach
}
protected virtual void CompileProperty(object container, PropertyInfo property,
#if FW2
List<string> fields
#else
ArrayList fields
#endif
)
{
QueryStringFieldAttribute queryStringField =
(QueryStringFieldAttribute)Attribute.GetCustomAttribute(property, typeof(QueryStringFieldAttribute));
string propertyValue = CompilePropertyValue(container, property, queryStringField);
if(propertyValue != null && propertyValue != string.Empty)
fields.Add(string.Format("{0}={1}", queryStringField.Name, propertyValue));
}
protected virtual string CompilePropertyValue(object container, PropertyInfo property,
QueryStringFieldAttribute queryStringField)
{
if(property.PropertyType.IsArray)
return CompileArrayPropertyValue(container, property);
if(property.PropertyType.IsAssignableFrom(typeof(DateTime)))
return CompileDateTimePropertyValue(container, property, queryStringField);
if(typeof(IList).IsAssignableFrom(property.PropertyType))
return CompileListPropertyValue(container, property, queryStringField);
if(typeof(bool) == property.PropertyType)
return CompileBooleanPropertyValue(container, property, queryStringField);
object value = property.GetValue(container, null);
return value == null ? null : value.ToString();
}
private string CompileBooleanPropertyValue(object container, PropertyInfo property,
QueryStringFieldAttribute queryStringField)
{
object value = property.GetValue(container, null);
if(value == null)
return null;
return (bool)value ? "1" : "0";
}
private static string CompileListPropertyValue(object container, PropertyInfo property,
QueryStringFieldAttribute queryStringField)
{
IList list = (IList)property.GetValue(container, null);
if(list == null || list.Count == 0)
return null;
#if FW2
List<string> values = new List<string>();
#else
ArrayList values = new ArrayList();
#endif
foreach(object value in list)
values.Add(value.ToString());
#if FW2
return string.Join(",", values.ToArray());
#else
return string.Join(",", (string[])values.ToArray(typeof(string)));
#endif
}
protected string CompileDateTimePropertyValue(object container,
PropertyInfo property, QueryStringFieldAttribute queryStringField)
{
object value = property.GetValue(container, null);
if(value == null)
return null;
DateTime dateTime = (DateTime)value;
return dateTime.ToString(queryStringField.DateTimeFormatString);
}
private static string CompileArrayPropertyValue(object container, PropertyInfo property)
{
IEnumerable array = (IEnumerable)property.GetValue(container, null);
#if FW2
List<string> values = new List<string>();
#else
ArrayList values = new ArrayList();
#endif
foreach(object value in array)
values.Add(value.ToString());
#if FW2
return string.Join(",", values.ToArray());
#else
return string.Join(",", (string[])values.ToArray(typeof(string)));
#endif
}
}
}
Show details
Hide details
Change log
r159
by anton.gogolev on Dec 19, 2007
Diff
[No log message]
Go to:
...ueryStringCompilerTestFixture.cs
...ests/Web/QueryStringContainer.cs
.../QueryStringParserTestFixture.cs
...viceProxyGeneratorTestFixture.cs
...ushie.Web/QueryStringCompiler.cs
...Brushie.Web/QueryStringParser.cs
...rialization/ArrayDeserializer.cs
Project members,
sign in
to write a code review
Older revisions
r158
by anton.gogolev on Dec 18, 2007
Diff
[No log message]
r157
by anton.gogolev on Dec 14, 2007
Diff
[No log message]
r154
by anton.gogolev on Dec 13, 2007
Diff
[No log message]
All revisions of this file
File info
Size: 5416 bytes, 164 lines
View raw file
Hosted by