1
1
using System ;
2
- using System . Linq ;
3
2
using System . Collections . Generic ;
4
3
using ServiceStack . Text . Common ;
5
- using System . Reflection ;
6
4
7
5
namespace ServiceStack . Text
8
6
{
9
7
public static class CsvConfig
10
8
{
11
- static CsvConfig ( )
12
- {
9
+ static CsvConfig ( )
10
+ {
13
11
Reset ( ) ;
14
- }
15
-
16
- [ ThreadStatic ]
17
- private static string tsItemSeperatorString ;
18
- private static string sItemSeperatorString ;
19
- public static string ItemSeperatorString
20
- {
21
- get
22
- {
23
- return tsItemSeperatorString ?? sItemSeperatorString ?? JsWriter . ItemSeperatorString ;
24
- }
25
- set
26
- {
27
- tsItemSeperatorString = value ;
28
- if ( sItemSeperatorString == null ) sItemSeperatorString = value ;
12
+ }
13
+
14
+ [ ThreadStatic ]
15
+ private static string tsItemSeperatorString ;
16
+ private static string sItemSeperatorString ;
17
+ public static string ItemSeperatorString
18
+ {
19
+ get
20
+ {
21
+ return tsItemSeperatorString ?? sItemSeperatorString ?? JsWriter . ItemSeperatorString ;
22
+ }
23
+ set
24
+ {
25
+ tsItemSeperatorString = value ;
26
+ if ( sItemSeperatorString == null ) sItemSeperatorString = value ;
29
27
ResetEscapeStrings ( ) ;
30
- }
31
- }
32
-
33
- [ ThreadStatic ]
34
- private static string tsItemDelimiterString ;
35
- private static string sItemDelimiterString ;
36
- public static string ItemDelimiterString
37
- {
38
- get
39
- {
40
- return tsItemDelimiterString ?? sItemDelimiterString ?? JsWriter . QuoteString ;
41
- }
42
- set
43
- {
44
- tsItemDelimiterString = value ;
45
- if ( sItemDelimiterString == null ) sItemDelimiterString = value ;
46
- EscapedItemDelimiterString = value + value ;
47
- ResetEscapeStrings ( ) ;
48
- }
49
- }
28
+ }
29
+ }
30
+
31
+ [ ThreadStatic ]
32
+ private static string tsItemDelimiterString ;
33
+ private static string sItemDelimiterString ;
34
+ public static string ItemDelimiterString
35
+ {
36
+ get
37
+ {
38
+ return tsItemDelimiterString ?? sItemDelimiterString ?? JsWriter . QuoteString ;
39
+ }
40
+ set
41
+ {
42
+ tsItemDelimiterString = value ;
43
+ if ( sItemDelimiterString == null ) sItemDelimiterString = value ;
44
+ EscapedItemDelimiterString = value + value ;
45
+ ResetEscapeStrings ( ) ;
46
+ }
47
+ }
50
48
51
49
private const string DefaultEscapedItemDelimiterString = JsWriter . QuoteString + JsWriter . QuoteString ;
52
50
53
51
[ ThreadStatic ]
54
- private static string tsEscapedItemDelimiterString ;
55
- private static string sEscapedItemDelimiterString ;
56
- internal static string EscapedItemDelimiterString
57
- {
58
- get
59
- {
60
- return tsEscapedItemDelimiterString ?? sEscapedItemDelimiterString ?? DefaultEscapedItemDelimiterString ;
61
- }
62
- set
63
- {
64
- tsEscapedItemDelimiterString = value ;
65
- if ( sEscapedItemDelimiterString == null ) sEscapedItemDelimiterString = value ;
66
- }
67
- }
52
+ private static string tsEscapedItemDelimiterString ;
53
+ private static string sEscapedItemDelimiterString ;
54
+ internal static string EscapedItemDelimiterString
55
+ {
56
+ get
57
+ {
58
+ return tsEscapedItemDelimiterString ?? sEscapedItemDelimiterString ?? DefaultEscapedItemDelimiterString ;
59
+ }
60
+ set
61
+ {
62
+ tsEscapedItemDelimiterString = value ;
63
+ if ( sEscapedItemDelimiterString == null ) sEscapedItemDelimiterString = value ;
64
+ }
65
+ }
68
66
69
67
private static readonly string [ ] defaultEscapeStrings = GetEscapeStrings ( ) ;
70
68
71
- [ ThreadStatic ]
72
- private static string [ ] tsEscapeStrings ;
73
- private static string [ ] sEscapeStrings ;
74
- public static string [ ] EscapeStrings
75
- {
76
- get
77
- {
78
- return tsEscapeStrings ?? sEscapeStrings ?? defaultEscapeStrings ;
79
- }
69
+ [ ThreadStatic ]
70
+ private static string [ ] tsEscapeStrings ;
71
+ private static string [ ] sEscapeStrings ;
72
+ public static string [ ] EscapeStrings
73
+ {
74
+ get
75
+ {
76
+ return tsEscapeStrings ?? sEscapeStrings ?? defaultEscapeStrings ;
77
+ }
80
78
private set
81
79
{
82
- tsEscapeStrings = value ;
83
- if ( sEscapeStrings == null ) sEscapeStrings = value ;
80
+ tsEscapeStrings = value ;
81
+ if ( sEscapeStrings == null ) sEscapeStrings = value ;
84
82
}
85
- }
83
+ }
86
84
87
85
private static string [ ] GetEscapeStrings ( )
88
86
{
89
- return new [ ] { ItemDelimiterString , ItemSeperatorString , RowSeparatorString , "\r " , "\n " } ;
87
+ return new [ ] { ItemDelimiterString , ItemSeperatorString , RowSeparatorString , "\r " , "\n " } ;
90
88
}
91
89
92
90
private static void ResetEscapeStrings ( )
93
91
{
94
92
EscapeStrings = GetEscapeStrings ( ) ;
95
93
}
96
94
97
- [ ThreadStatic ]
98
- private static string tsRowSeparatorString ;
99
- private static string sRowSeparatorString ;
100
- public static string RowSeparatorString
101
- {
102
- get
103
- {
104
- return tsRowSeparatorString ?? sRowSeparatorString ?? Environment . NewLine ;
105
- }
106
- set
107
- {
108
- tsRowSeparatorString = value ;
109
- if ( sRowSeparatorString == null ) sRowSeparatorString = value ;
95
+ [ ThreadStatic ]
96
+ private static string tsRowSeparatorString ;
97
+ private static string sRowSeparatorString ;
98
+ public static string RowSeparatorString
99
+ {
100
+ get
101
+ {
102
+ return tsRowSeparatorString ?? sRowSeparatorString ?? Environment . NewLine ;
103
+ }
104
+ set
105
+ {
106
+ tsRowSeparatorString = value ;
107
+ if ( sRowSeparatorString == null ) sRowSeparatorString = value ;
110
108
ResetEscapeStrings ( ) ;
111
- }
112
- }
113
-
114
- public static void Reset ( )
115
- {
116
- tsItemSeperatorString = sItemSeperatorString = null ;
117
- tsItemDelimiterString = sItemDelimiterString = null ;
118
- tsEscapedItemDelimiterString = sEscapedItemDelimiterString = null ;
119
- tsRowSeparatorString = sRowSeparatorString = null ;
120
- tsEscapeStrings = sEscapeStrings = null ;
121
- }
109
+ }
110
+ }
111
+
112
+ public static void Reset ( )
113
+ {
114
+ tsItemSeperatorString = sItemSeperatorString = null ;
115
+ tsItemDelimiterString = sItemDelimiterString = null ;
116
+ tsEscapedItemDelimiterString = sEscapedItemDelimiterString = null ;
117
+ tsRowSeparatorString = sRowSeparatorString = null ;
118
+ tsEscapeStrings = sEscapeStrings = null ;
119
+ }
122
120
123
121
}
124
122
125
- public static class CsvConfig < T >
126
- {
127
- public static bool OmitHeaders { get ; set ; }
128
-
129
- private static Dictionary < string , string > customHeadersMap ;
130
- public static Dictionary < string , string > CustomHeadersMap
131
- {
132
- get
133
- {
134
- return customHeadersMap ;
135
- }
136
- set
137
- {
138
- customHeadersMap = value ;
139
- if ( value == null ) return ;
140
- CsvWriter < T > . ConfigureCustomHeaders ( customHeadersMap ) ;
141
- }
142
- }
143
-
144
- public static object CustomHeaders
145
- {
146
- set
147
- {
148
- if ( value == null ) return ;
123
+ public static class CsvConfig < T >
124
+ {
125
+ public static bool OmitHeaders { get ; set ; }
126
+
127
+ private static Dictionary < string , string > customHeadersMap ;
128
+ public static Dictionary < string , string > CustomHeadersMap
129
+ {
130
+ get
131
+ {
132
+ return customHeadersMap ;
133
+ }
134
+ set
135
+ {
136
+ customHeadersMap = value ;
137
+ if ( value == null ) return ;
138
+ CsvWriter < T > . ConfigureCustomHeaders ( customHeadersMap ) ;
139
+ }
140
+ }
141
+
142
+ public static object CustomHeaders
143
+ {
144
+ set
145
+ {
146
+ if ( value == null ) return ;
149
147
if ( value . GetType ( ) . IsValueType ( ) )
150
148
throw new ArgumentException ( "CustomHeaders is a ValueType" ) ;
151
149
152
150
var propertyInfos = value . GetType ( ) . GetPropertyInfos ( ) ;
153
151
if ( propertyInfos . Length == 0 ) return ;
154
152
155
- customHeadersMap = new Dictionary < string , string > ( ) ;
156
- foreach ( var pi in propertyInfos )
157
- {
153
+ customHeadersMap = new Dictionary < string , string > ( ) ;
154
+ foreach ( var pi in propertyInfos )
155
+ {
158
156
var getMethod = pi . GetMethodInfo ( ) ;
159
157
if ( getMethod == null ) continue ;
160
158
161
- var oValue = getMethod . Invoke ( value , new object [ 0 ] ) ;
162
- if ( oValue == null ) continue ;
163
- customHeadersMap [ pi . Name ] = oValue . ToString ( ) ;
164
- }
165
- CsvWriter < T > . ConfigureCustomHeaders ( customHeadersMap ) ;
166
- }
167
- }
168
-
169
- public static void Reset ( )
170
- {
171
- OmitHeaders = false ;
172
- CsvWriter < T > . Reset ( ) ;
173
- }
174
- }
159
+ var oValue = getMethod . Invoke ( value , new object [ 0 ] ) ;
160
+ if ( oValue == null ) continue ;
161
+ customHeadersMap [ pi . Name ] = oValue . ToString ( ) ;
162
+ }
163
+ CsvWriter < T > . ConfigureCustomHeaders ( customHeadersMap ) ;
164
+ }
165
+ }
166
+
167
+ public static void Reset ( )
168
+ {
169
+ OmitHeaders = false ;
170
+ CsvWriter < T > . Reset ( ) ;
171
+ }
172
+ }
175
173
}
0 commit comments