Mercurial > pub > bltoolkit
comparison Source/Common/Convert.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Data.Linq; | |
3 using System.Data.SqlTypes; | |
4 using System.IO; | |
5 using System.Threading; | |
6 using System.Xml; | |
7 #if !SILVERLIGHT | |
8 using System.Xml.Linq; | |
9 #endif | |
10 | |
11 namespace BLToolkit.Common | |
12 { | |
13 using Properties; | |
14 | |
15 /// <summary>Converts a base data type to another base data type.</summary> | |
16 public static partial class Convert | |
17 { | |
18 #region Boolean | |
19 | |
20 /// <summary>Converts the value from <c>Char</c> to an equivalent <c>Boolean</c> value.</summary> | |
21 public static Boolean ToBoolean(Char p) | |
22 { | |
23 switch (p) | |
24 { | |
25 case '\x0' : // Allow int <=> Char <=> Boolean | |
26 case '0' : | |
27 case 'n' : | |
28 case 'N' : | |
29 case 'f' : | |
30 case 'F' : return false; | |
31 | |
32 case '\x1' : // Allow int <=> Char <=> Boolean | |
33 case '1' : | |
34 case 'y' : | |
35 case 'Y' : | |
36 case 't' : | |
37 case 'T' : return true; | |
38 } | |
39 | |
40 throw CreateInvalidCastException(typeof(Char), typeof(Boolean)); | |
41 } | |
42 | |
43 #endregion | |
44 | |
45 #region Byte[] | |
46 | |
47 /// <summary>Converts the value from <c>Decimal</c> to an equivalent <c>Byte[]</c> value.</summary> | |
48 public static Byte[] ToByteArray(Decimal p) | |
49 { | |
50 var bits = Decimal.GetBits(p); | |
51 var bytes = new Byte[Buffer.ByteLength(bits)]; | |
52 | |
53 Buffer.BlockCopy(bits, 0, bytes, 0, bytes.Length); | |
54 return bytes; | |
55 } | |
56 | |
57 /// <summary>Converts the value from <c>Stream</c> to an equivalent <c>Byte[]</c> value.</summary> | |
58 public static Byte[] ToByteArray(Stream p) | |
59 { | |
60 if (p == null || p == Stream.Null) return null; | |
61 if (p is MemoryStream) return ((MemoryStream)p).ToArray(); | |
62 | |
63 var position = p.Seek(0, SeekOrigin.Begin); | |
64 var bytes = new Byte[p.Length]; | |
65 | |
66 p.Read(bytes, 0, bytes.Length); | |
67 p.Position = position; | |
68 | |
69 return bytes; | |
70 } | |
71 | |
72 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>Byte[]</c> value.</summary> | |
73 public static Byte[] ToByteArray(Char[] p) | |
74 { | |
75 var bytes = new Byte[Buffer.ByteLength(p)]; | |
76 | |
77 Buffer.BlockCopy(p, 0, bytes, 0, bytes.Length); | |
78 return bytes; | |
79 } | |
80 | |
81 #endregion | |
82 | |
83 #region Decimal | |
84 | |
85 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>Decimal</c> value.</summary> | |
86 public static Decimal ToDecimal(Byte[] p) | |
87 { | |
88 if (p == null || p.Length == 0) return 0.0m; | |
89 | |
90 var bits = new int[p.Length / sizeof(int)]; | |
91 | |
92 Buffer.BlockCopy(p, 0, bits, 0, p.Length); | |
93 return new Decimal(bits); | |
94 } | |
95 | |
96 public static Decimal ToDecimal(Binary p) | |
97 { | |
98 if (p == null || p.Length == 0) return 0.0m; | |
99 | |
100 var bits = new int[p.Length / sizeof(int)]; | |
101 | |
102 Buffer.BlockCopy(p.ToArray(), 0, bits, 0, p.Length); | |
103 return new Decimal(bits); | |
104 } | |
105 | |
106 #endregion | |
107 | |
108 #region SqlTypes | |
109 | |
110 #if !SILVERLIGHT | |
111 | |
112 #region SqlChars | |
113 | |
114 // Scalar Types. | |
115 // | |
116 /// <summary>Converts the value from <c>String</c> to an equivalent <c>SqlChars</c> value.</summary> | |
117 public static SqlChars ToSqlChars(String p) { return p == null? SqlChars.Null: new SqlChars(p.ToCharArray()); } | |
118 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>SqlChars</c> value.</summary> | |
119 public static SqlChars ToSqlChars(Char[] p) { return p == null? SqlChars.Null: new SqlChars(p); } | |
120 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>SqlChars</c> value.</summary> | |
121 public static SqlChars ToSqlChars(Byte[] p) { return p == null? SqlChars.Null: new SqlChars(ToCharArray(p)); } | |
122 public static SqlChars ToSqlChars(Binary p) { return p == null? SqlChars.Null: new SqlChars(ToCharArray(p.ToArray())); } | |
123 | |
124 /// <summary>Converts the value from <c>SByte</c> to an equivalent <c>SqlChars</c> value.</summary> | |
125 [CLSCompliant(false)] | |
126 public static SqlChars ToSqlChars(SByte p) { return new SqlChars(ToString(p).ToCharArray()); } | |
127 /// <summary>Converts the value from <c>Int16</c> to an equivalent <c>SqlChars</c> value.</summary> | |
128 public static SqlChars ToSqlChars(Int16 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
129 /// <summary>Converts the value from <c>Int32</c> to an equivalent <c>SqlChars</c> value.</summary> | |
130 public static SqlChars ToSqlChars(Int32 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
131 /// <summary>Converts the value from <c>Int64</c> to an equivalent <c>SqlChars</c> value.</summary> | |
132 public static SqlChars ToSqlChars(Int64 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
133 | |
134 /// <summary>Converts the value from <c>Byte</c> to an equivalent <c>SqlChars</c> value.</summary> | |
135 public static SqlChars ToSqlChars(Byte p) { return new SqlChars(ToString(p).ToCharArray()); } | |
136 /// <summary>Converts the value from <c>UInt16</c> to an equivalent <c>SqlChars</c> value.</summary> | |
137 [CLSCompliant(false)] | |
138 public static SqlChars ToSqlChars(UInt16 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
139 /// <summary>Converts the value from <c>UInt32</c> to an equivalent <c>SqlChars</c> value.</summary> | |
140 [CLSCompliant(false)] | |
141 public static SqlChars ToSqlChars(UInt32 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
142 /// <summary>Converts the value from <c>UInt64</c> to an equivalent <c>SqlChars</c> value.</summary> | |
143 [CLSCompliant(false)] | |
144 public static SqlChars ToSqlChars(UInt64 p) { return new SqlChars(ToString(p).ToCharArray()); } | |
145 | |
146 /// <summary>Converts the value from <c>Single</c> to an equivalent <c>SqlChars</c> value.</summary> | |
147 public static SqlChars ToSqlChars(Single p) { return new SqlChars(ToString(p).ToCharArray()); } | |
148 /// <summary>Converts the value from <c>Double</c> to an equivalent <c>SqlChars</c> value.</summary> | |
149 public static SqlChars ToSqlChars(Double p) { return new SqlChars(ToString(p).ToCharArray()); } | |
150 | |
151 /// <summary>Converts the value from <c>Boolean</c> to an equivalent <c>SqlChars</c> value.</summary> | |
152 public static SqlChars ToSqlChars(Boolean p) { return new SqlChars(ToString(p).ToCharArray()); } | |
153 /// <summary>Converts the value from <c>Decimal</c> to an equivalent <c>SqlChars</c> value.</summary> | |
154 public static SqlChars ToSqlChars(Decimal p) { return new SqlChars(ToString(p).ToCharArray()); } | |
155 /// <summary>Converts the value from <c>Char</c> to an equivalent <c>SqlChars</c> value.</summary> | |
156 public static SqlChars ToSqlChars(Char p) { return new SqlChars(ToString(p).ToCharArray()); } | |
157 /// <summary>Converts the value from <c>TimeSpan</c> to an equivalent <c>SqlChars</c> value.</summary> | |
158 public static SqlChars ToSqlChars(TimeSpan p) { return new SqlChars(ToString(p).ToCharArray()); } | |
159 /// <summary>Converts the value from <c>DateTime</c> to an equivalent <c>SqlChars</c> value.</summary> | |
160 public static SqlChars ToSqlChars(DateTime p) { return new SqlChars(ToString(p).ToCharArray()); } | |
161 /// <summary>Converts the value from <c>DateTimeOffset</c> to an equivalent <c>SqlChars</c> value.</summary> | |
162 public static SqlChars ToSqlChars(DateTimeOffset p) { return new SqlChars(ToString(p).ToCharArray()); } | |
163 /// <summary>Converts the value from <c>Guid</c> to an equivalent <c>SqlChars</c> value.</summary> | |
164 public static SqlChars ToSqlChars(Guid p) { return new SqlChars(ToString(p).ToCharArray()); } | |
165 | |
166 // Nullable Types. | |
167 // | |
168 /// <summary>Converts the value from <c>SByte?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
169 [CLSCompliant(false)] | |
170 public static SqlChars ToSqlChars(SByte? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
171 /// <summary>Converts the value from <c>Int16?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
172 public static SqlChars ToSqlChars(Int16? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
173 /// <summary>Converts the value from <c>Int32?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
174 public static SqlChars ToSqlChars(Int32? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
175 /// <summary>Converts the value from <c>Int64?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
176 public static SqlChars ToSqlChars(Int64? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
177 | |
178 /// <summary>Converts the value from <c>Byte?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
179 public static SqlChars ToSqlChars(Byte? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
180 /// <summary>Converts the value from <c>UInt16?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
181 [CLSCompliant(false)] | |
182 public static SqlChars ToSqlChars(UInt16? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
183 /// <summary>Converts the value from <c>UInt32?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
184 [CLSCompliant(false)] | |
185 public static SqlChars ToSqlChars(UInt32? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
186 /// <summary>Converts the value from <c>UInt64?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
187 [CLSCompliant(false)] | |
188 public static SqlChars ToSqlChars(UInt64? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
189 | |
190 /// <summary>Converts the value from <c>Single?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
191 public static SqlChars ToSqlChars(Single? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
192 /// <summary>Converts the value from <c>Double?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
193 public static SqlChars ToSqlChars(Double? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
194 | |
195 /// <summary>Converts the value from <c>Boolean?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
196 public static SqlChars ToSqlChars(Boolean? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
197 /// <summary>Converts the value from <c>Decimal?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
198 public static SqlChars ToSqlChars(Decimal? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
199 /// <summary>Converts the value from <c>Char?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
200 public static SqlChars ToSqlChars(Char? p) { return p.HasValue? new SqlChars(new Char[]{p.Value}) : SqlChars.Null; } | |
201 /// <summary>Converts the value from <c>TimeSpan?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
202 public static SqlChars ToSqlChars(TimeSpan? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
203 /// <summary>Converts the value from <c>DateTime?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
204 public static SqlChars ToSqlChars(DateTime? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
205 /// <summary>Converts the value from <c>DateTimeOffset?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
206 public static SqlChars ToSqlChars(DateTimeOffset? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
207 /// <summary>Converts the value from <c>Guid?</c> to an equivalent <c>SqlChars</c> value.</summary> | |
208 public static SqlChars ToSqlChars(Guid? p) { return p.HasValue? new SqlChars(p.ToString().ToCharArray()): SqlChars.Null; } | |
209 | |
210 // SqlTypes | |
211 // | |
212 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>SqlChars</c> value.</summary> | |
213 public static SqlChars ToSqlChars(SqlString p) { return (SqlChars)p; } | |
214 | |
215 /// <summary>Converts the value from <c>SqlByte</c> to an equivalent <c>SqlChars</c> value.</summary> | |
216 public static SqlChars ToSqlChars(SqlByte p) { return (SqlChars)p.ToSqlString(); } | |
217 /// <summary>Converts the value from <c>SqlInt16</c> to an equivalent <c>SqlChars</c> value.</summary> | |
218 public static SqlChars ToSqlChars(SqlInt16 p) { return (SqlChars)p.ToSqlString(); } | |
219 /// <summary>Converts the value from <c>SqlInt32</c> to an equivalent <c>SqlChars</c> value.</summary> | |
220 public static SqlChars ToSqlChars(SqlInt32 p) { return (SqlChars)p.ToSqlString(); } | |
221 /// <summary>Converts the value from <c>SqlInt64</c> to an equivalent <c>SqlChars</c> value.</summary> | |
222 public static SqlChars ToSqlChars(SqlInt64 p) { return (SqlChars)p.ToSqlString(); } | |
223 | |
224 /// <summary>Converts the value from <c>SqlSingle</c> to an equivalent <c>SqlChars</c> value.</summary> | |
225 public static SqlChars ToSqlChars(SqlSingle p) { return (SqlChars)p.ToSqlString(); } | |
226 /// <summary>Converts the value from <c>SqlDouble</c> to an equivalent <c>SqlChars</c> value.</summary> | |
227 public static SqlChars ToSqlChars(SqlDouble p) { return (SqlChars)p.ToSqlString(); } | |
228 /// <summary>Converts the value from <c>SqlDecimal</c> to an equivalent <c>SqlChars</c> value.</summary> | |
229 public static SqlChars ToSqlChars(SqlDecimal p) { return (SqlChars)p.ToSqlString(); } | |
230 /// <summary>Converts the value from <c>SqlMoney</c> to an equivalent <c>SqlChars</c> value.</summary> | |
231 public static SqlChars ToSqlChars(SqlMoney p) { return (SqlChars)p.ToSqlString(); } | |
232 | |
233 /// <summary>Converts the value from <c>SqlBoolean</c> to an equivalent <c>SqlChars</c> value.</summary> | |
234 public static SqlChars ToSqlChars(SqlBoolean p) { return (SqlChars)p.ToSqlString(); } | |
235 /// <summary>Converts the value from <c>SqlGuid</c> to an equivalent <c>SqlChars</c> value.</summary> | |
236 public static SqlChars ToSqlChars(SqlGuid p) { return (SqlChars)p.ToSqlString(); } | |
237 /// <summary>Converts the value from <c>SqlDateTime</c> to an equivalent <c>SqlChars</c> value.</summary> | |
238 public static SqlChars ToSqlChars(SqlDateTime p) { return (SqlChars)p.ToSqlString(); } | |
239 /// <summary>Converts the value from <c>SqlBinary</c> to an equivalent <c>SqlChars</c> value.</summary> | |
240 public static SqlChars ToSqlChars(SqlBinary p) { return p.IsNull? SqlChars.Null: new SqlChars(p.ToString().ToCharArray()); } | |
241 | |
242 /// <summary>Converts the value from <c>Type</c> to an equivalent <c>SqlChars</c> value.</summary> | |
243 public static SqlChars ToSqlChars(Type p) { return p == null? SqlChars.Null: new SqlChars(p.FullName.ToCharArray()); } | |
244 /// <summary>Converts the value of a specified object to an equivalent <c>SqlChars</c> value.</summary> | |
245 public static SqlChars ToSqlChars(object p) | |
246 { | |
247 if (p == null || p is DBNull) return SqlChars.Null; | |
248 | |
249 if (p is SqlChars) return (SqlChars)p; | |
250 | |
251 // Scalar Types. | |
252 // | |
253 if (p is String) return ToSqlChars((String)p); | |
254 if (p is Char[]) return ToSqlChars((Char[])p); | |
255 if (p is Byte[]) return ToSqlChars((Byte[])p); | |
256 if (p is Binary) return ToSqlChars(((Binary)p).ToArray()); | |
257 | |
258 if (p is SByte) return ToSqlChars((SByte)p); | |
259 if (p is Int16) return ToSqlChars((Int16)p); | |
260 if (p is Int32) return ToSqlChars((Int32)p); | |
261 if (p is Int64) return ToSqlChars((Int64)p); | |
262 | |
263 if (p is Byte) return ToSqlChars((Byte)p); | |
264 if (p is UInt16) return ToSqlChars((UInt16)p); | |
265 if (p is UInt32) return ToSqlChars((UInt32)p); | |
266 if (p is UInt64) return ToSqlChars((UInt64)p); | |
267 | |
268 if (p is Single) return ToSqlChars((Single)p); | |
269 if (p is Double) return ToSqlChars((Double)p); | |
270 | |
271 if (p is Boolean) return ToSqlChars((Boolean)p); | |
272 if (p is Decimal) return ToSqlChars((Decimal)p); | |
273 | |
274 // SqlTypes | |
275 // | |
276 if (p is SqlString) return ToSqlChars((SqlString)p); | |
277 | |
278 if (p is SqlByte) return ToSqlChars((SqlByte)p); | |
279 if (p is SqlInt16) return ToSqlChars((SqlInt16)p); | |
280 if (p is SqlInt32) return ToSqlChars((SqlInt32)p); | |
281 if (p is SqlInt64) return ToSqlChars((SqlInt64)p); | |
282 | |
283 if (p is SqlSingle) return ToSqlChars((SqlSingle)p); | |
284 if (p is SqlDouble) return ToSqlChars((SqlDouble)p); | |
285 if (p is SqlDecimal) return ToSqlChars((SqlDecimal)p); | |
286 if (p is SqlMoney) return ToSqlChars((SqlMoney)p); | |
287 | |
288 if (p is SqlBoolean) return ToSqlChars((SqlBoolean)p); | |
289 if (p is SqlBinary) return ToSqlChars((SqlBinary)p); | |
290 if (p is Type) return ToSqlChars((Type)p); | |
291 | |
292 return new SqlChars(ToString(p).ToCharArray()); | |
293 } | |
294 | |
295 #endregion | |
296 | |
297 #endif | |
298 | |
299 #endregion | |
300 | |
301 #region Other Types | |
302 | |
303 #region Binary | |
304 | |
305 // Scalar Types. | |
306 // | |
307 /// <summary>Converts the value from <c>String</c> to an equivalent <c>Byte[]</c> value.</summary> | |
308 public static Binary ToLinqBinary(String p) { return p == null? null: new Binary(System.Text.Encoding.UTF8.GetBytes(p)); } | |
309 /// <summary>Converts the value from <c>Byte</c> to an equivalent <c>Byte[]</c> value.</summary> | |
310 public static Binary ToLinqBinary(Byte p) { return new Binary(new byte[]{p}); } | |
311 /// <summary>Converts the value from <c>SByte</c> to an equivalent <c>Byte[]</c> value.</summary> | |
312 [CLSCompliant(false)] | |
313 public static Binary ToLinqBinary(SByte p) { return new Binary(new byte[]{checked((Byte)p)}); } | |
314 /// <summary>Converts the value from <c>Decimal</c> to an equivalent <c>Byte[]</c> value.</summary> | |
315 public static Binary ToLinqBinary(Decimal p) | |
316 { | |
317 var bits = Decimal.GetBits(p); | |
318 var bytes = new Byte[Buffer.ByteLength(bits)]; | |
319 | |
320 Buffer.BlockCopy(bits, 0, bytes, 0, bytes.Length); | |
321 return new Binary(bytes); | |
322 } | |
323 /// <summary>Converts the value from <c>Int16</c> to an equivalent <c>Byte[]</c> value.</summary> | |
324 public static Binary ToLinqBinary(Int16 p) { return new Binary(BitConverter.GetBytes(p)); } | |
325 /// <summary>Converts the value from <c>Int32</c> to an equivalent <c>Byte[]</c> value.</summary> | |
326 public static Binary ToLinqBinary(Int32 p) { return new Binary(BitConverter.GetBytes(p)); } | |
327 /// <summary>Converts the value from <c>Int64</c> to an equivalent <c>Byte[]</c> value.</summary> | |
328 public static Binary ToLinqBinary(Int64 p) { return new Binary(BitConverter.GetBytes(p)); } | |
329 | |
330 /// <summary>Converts the value from <c>UInt16</c> to an equivalent <c>Byte[]</c> value.</summary> | |
331 [CLSCompliant(false)] | |
332 public static Binary ToLinqBinary(UInt16 p) { return new Binary(BitConverter.GetBytes(p)); } | |
333 /// <summary>Converts the value from <c>UInt32</c> to an equivalent <c>Byte[]</c> value.</summary> | |
334 [CLSCompliant(false)] | |
335 public static Binary ToLinqBinary(UInt32 p) { return new Binary(BitConverter.GetBytes(p)); } | |
336 /// <summary>Converts the value from <c>UInt64</c> to an equivalent <c>Byte[]</c> value.</summary> | |
337 [CLSCompliant(false)] | |
338 public static Binary ToLinqBinary(UInt64 p) { return new Binary(BitConverter.GetBytes(p)); } | |
339 | |
340 /// <summary>Converts the value from <c>Single</c> to an equivalent <c>Byte[]</c> value.</summary> | |
341 public static Binary ToLinqBinary(Single p) { return new Binary(BitConverter.GetBytes(p)); } | |
342 /// <summary>Converts the value from <c>Double</c> to an equivalent <c>Byte[]</c> value.</summary> | |
343 public static Binary ToLinqBinary(Double p) { return new Binary(BitConverter.GetBytes(p)); } | |
344 | |
345 /// <summary>Converts the value from <c>Boolean</c> to an equivalent <c>Byte[]</c> value.</summary> | |
346 public static Binary ToLinqBinary(Boolean p) { return new Binary(BitConverter.GetBytes(p)); } | |
347 /// <summary>Converts the value from <c>Char</c> to an equivalent <c>Byte[]</c> value.</summary> | |
348 public static Binary ToLinqBinary(Char p) { return new Binary(BitConverter.GetBytes(p)); } | |
349 #if !SILVERLIGHT | |
350 /// <summary>Converts the value from <c>DateTime</c> to an equivalent <c>Byte[]</c> value.</summary> | |
351 public static Binary ToLinqBinary(DateTime p) { return new Binary(ToByteArray(p.ToBinary())); } | |
352 /// <summary>Converts the value from <c>DateTimeOffset</c> to an equivalent <c>Byte[]</c> value.</summary> | |
353 public static Binary ToLinqBinary(DateTimeOffset p) { return new Binary(ToByteArray(p.LocalDateTime.ToBinary())); } | |
354 #endif | |
355 public static Binary ToLinqBinary(Byte[] p) { return new Binary(p); } | |
356 /// <summary>Converts the value from <c>TimeSpan</c> to an equivalent <c>Byte[]</c> value.</summary> | |
357 public static Binary ToLinqBinary(TimeSpan p) { return new Binary(ToByteArray(p.Ticks)); } | |
358 /// <summary>Converts the value from <c>Stream</c> to an equivalent <c>Byte[]</c> value.</summary> | |
359 public static Binary ToLinqBinary(Stream p) | |
360 { | |
361 if (p == null) return null; | |
362 if (p is MemoryStream) return ((MemoryStream)p).ToArray(); | |
363 | |
364 var position = p.Seek(0, SeekOrigin.Begin); | |
365 var bytes = new Byte[p.Length]; | |
366 p.Read(bytes, 0, bytes.Length); | |
367 p.Position = position; | |
368 | |
369 return new Binary(bytes); | |
370 } | |
371 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>Byte[]</c> value.</summary> | |
372 public static Binary ToLinqBinary(Char[] p) | |
373 { | |
374 var bytes = new Byte[Buffer.ByteLength(p)]; | |
375 | |
376 Buffer.BlockCopy(p, 0, bytes, 0, bytes.Length); | |
377 return new Binary(bytes); | |
378 } | |
379 /// <summary>Converts the value from <c>Guid</c> to an equivalent <c>Byte[]</c> value.</summary> | |
380 public static Binary ToLinqBinary(Guid p) { return p == Guid.Empty? null: new Binary(p.ToByteArray()); } | |
381 | |
382 // Nullable Types. | |
383 // | |
384 /// <summary>Converts the value from <c>SByte?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
385 [CLSCompliant(false)] | |
386 public static Binary ToLinqBinary(SByte? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
387 /// <summary>Converts the value from <c>Int16?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
388 public static Binary ToLinqBinary(Int16? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
389 /// <summary>Converts the value from <c>Int32?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
390 public static Binary ToLinqBinary(Int32? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
391 /// <summary>Converts the value from <c>Int64?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
392 public static Binary ToLinqBinary(Int64? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
393 | |
394 /// <summary>Converts the value from <c>Byte?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
395 public static Binary ToLinqBinary(Byte? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
396 /// <summary>Converts the value from <c>UInt16?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
397 [CLSCompliant(false)] | |
398 public static Binary ToLinqBinary(UInt16? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
399 /// <summary>Converts the value from <c>UInt32?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
400 [CLSCompliant(false)] | |
401 public static Binary ToLinqBinary(UInt32? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
402 /// <summary>Converts the value from <c>UInt64?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
403 [CLSCompliant(false)] | |
404 public static Binary ToLinqBinary(UInt64? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
405 | |
406 /// <summary>Converts the value from <c>Single?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
407 public static Binary ToLinqBinary(Single? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
408 /// <summary>Converts the value from <c>Double?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
409 public static Binary ToLinqBinary(Double? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
410 | |
411 /// <summary>Converts the value from <c>Boolean?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
412 public static Binary ToLinqBinary(Boolean? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
413 /// <summary>Converts the value from <c>Decimal?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
414 public static Binary ToLinqBinary(Decimal? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
415 /// <summary>Converts the value from <c>Char?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
416 public static Binary ToLinqBinary(Char? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
417 /// <summary>Converts the value from <c>DateTime?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
418 public static Binary ToLinqBinary(DateTime? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
419 /// <summary>Converts the value from <c>DateTimeOffset?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
420 public static Binary ToLinqBinary(DateTimeOffset? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
421 /// <summary>Converts the value from <c>TimeSpan?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
422 public static Binary ToLinqBinary(TimeSpan? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
423 /// <summary>Converts the value from <c>Guid?</c> to an equivalent <c>Byte[]</c> value.</summary> | |
424 public static Binary ToLinqBinary(Guid? p) { return p.HasValue? new Binary(ToByteArray(p.Value)): null; } | |
425 | |
426 #if !SILVERLIGHT | |
427 | |
428 // SqlTypes | |
429 // | |
430 /// <summary>Converts the value from <c>SqlBinary</c> to an equivalent <c>Byte[]</c> value.</summary> | |
431 public static Binary ToLinqBinary(SqlBinary p) { return p.IsNull? null: new Binary(p.Value); } | |
432 /// <summary>Converts the value from <c>SqlBytes</c> to an equivalent <c>Byte[]</c> value.</summary> | |
433 public static Binary ToLinqBinary(SqlBytes p) { return p.IsNull? null: new Binary(p.Value); } | |
434 /// <summary>Converts the value from <c>SqlGuid</c> to an equivalent <c>Byte[]</c> value.</summary> | |
435 public static Binary ToLinqBinary(SqlGuid p) { return p.IsNull? null: new Binary(p.ToByteArray()); } | |
436 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>Byte[]</c> value.</summary> | |
437 public static Binary ToLinqBinary(SqlString p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
438 | |
439 /// <summary>Converts the value from <c>SqlByte</c> to an equivalent <c>Byte[]</c> value.</summary> | |
440 public static Binary ToLinqBinary(SqlByte p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
441 /// <summary>Converts the value from <c>SqlInt16</c> to an equivalent <c>Byte[]</c> value.</summary> | |
442 public static Binary ToLinqBinary(SqlInt16 p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
443 /// <summary>Converts the value from <c>SqlInt32</c> to an equivalent <c>Byte[]</c> value.</summary> | |
444 public static Binary ToLinqBinary(SqlInt32 p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
445 /// <summary>Converts the value from <c>SqlInt64</c> to an equivalent <c>Byte[]</c> value.</summary> | |
446 public static Binary ToLinqBinary(SqlInt64 p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
447 | |
448 /// <summary>Converts the value from <c>SqlSingle</c> to an equivalent <c>Byte[]</c> value.</summary> | |
449 public static Binary ToLinqBinary(SqlSingle p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
450 /// <summary>Converts the value from <c>SqlDouble</c> to an equivalent <c>Byte[]</c> value.</summary> | |
451 public static Binary ToLinqBinary(SqlDouble p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
452 /// <summary>Converts the value from <c>SqlDecimal</c> to an equivalent <c>Byte[]</c> value.</summary> | |
453 public static Binary ToLinqBinary(SqlDecimal p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
454 /// <summary>Converts the value from <c>SqlMoney</c> to an equivalent <c>Byte[]</c> value.</summary> | |
455 public static Binary ToLinqBinary(SqlMoney p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
456 | |
457 /// <summary>Converts the value from <c>SqlBoolean</c> to an equivalent <c>Byte[]</c> value.</summary> | |
458 public static Binary ToLinqBinary(SqlBoolean p) { return p.IsNull? null: new Binary(ToByteArray(p.Value)); } | |
459 | |
460 #endif | |
461 | |
462 /// <summary>Converts the value of a specified object to an equivalent <c>Byte[]</c> value.</summary> | |
463 public static Binary ToLinqBinary(object p) | |
464 { | |
465 if (p == null || p is DBNull) return null; | |
466 | |
467 if (p is Byte[]) return new Binary((Byte[])p); | |
468 if (p is Binary) return (Binary)p; | |
469 | |
470 // Scalar Types. | |
471 // | |
472 if (p is String) return ToLinqBinary((String)p); | |
473 if (p is Byte) return ToLinqBinary((Byte)p); | |
474 if (p is SByte) return ToLinqBinary((SByte)p); | |
475 if (p is Decimal) return ToLinqBinary((Decimal)p); | |
476 if (p is Int16) return ToLinqBinary((Int16)p); | |
477 if (p is Int32) return ToLinqBinary((Int32)p); | |
478 if (p is Int64) return ToLinqBinary((Int64)p); | |
479 | |
480 if (p is UInt16) return ToLinqBinary((UInt16)p); | |
481 if (p is UInt32) return ToLinqBinary((UInt32)p); | |
482 if (p is UInt64) return ToLinqBinary((UInt64)p); | |
483 | |
484 if (p is Single) return ToLinqBinary((Single)p); | |
485 if (p is Double) return ToLinqBinary((Double)p); | |
486 | |
487 if (p is Boolean) return ToLinqBinary((Boolean)p); | |
488 if (p is DateTime) return ToLinqBinary((DateTime)p); | |
489 if (p is DateTimeOffset) return ToLinqBinary((DateTimeOffset)p); | |
490 if (p is TimeSpan) return ToLinqBinary((TimeSpan)p); | |
491 if (p is Stream) return ToLinqBinary((Stream)p); | |
492 if (p is Char[]) return ToLinqBinary((Char[])p); | |
493 if (p is Guid) return ToLinqBinary((Guid)p); | |
494 | |
495 #if !SILVERLIGHT | |
496 | |
497 // SqlTypes | |
498 // | |
499 if (p is SqlBinary) return ToLinqBinary((SqlBinary)p); | |
500 if (p is SqlBytes) return ToLinqBinary((SqlBytes)p); | |
501 if (p is SqlGuid) return ToLinqBinary((SqlGuid)p); | |
502 if (p is SqlString) return ToLinqBinary((SqlString)p); | |
503 | |
504 if (p is SqlByte) return ToLinqBinary((SqlByte)p); | |
505 if (p is SqlInt16) return ToLinqBinary((SqlInt16)p); | |
506 if (p is SqlInt32) return ToLinqBinary((SqlInt32)p); | |
507 if (p is SqlInt64) return ToLinqBinary((SqlInt64)p); | |
508 | |
509 if (p is SqlSingle) return ToLinqBinary((SqlSingle)p); | |
510 if (p is SqlDouble) return ToLinqBinary((SqlDouble)p); | |
511 if (p is SqlDecimal) return ToLinqBinary((SqlDecimal)p); | |
512 if (p is SqlMoney) return ToLinqBinary((SqlMoney)p); | |
513 | |
514 if (p is SqlBoolean) return ToLinqBinary((SqlBoolean)p); | |
515 | |
516 #endif | |
517 | |
518 throw CreateInvalidCastException(p.GetType(), typeof(Byte[])); | |
519 } | |
520 | |
521 #endregion | |
522 | |
523 #region Char[] | |
524 | |
525 // Scalar Types. | |
526 // | |
527 /// <summary>Converts the value from <c>String</c> to an equivalent <c>Char[]</c> value.</summary> | |
528 public static Char[] ToCharArray(String p) { return p == null? null: p.ToCharArray(); } | |
529 | |
530 #if !SILVERLIGHT | |
531 | |
532 // SqlTypes | |
533 // | |
534 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>Char[]</c> value.</summary> | |
535 public static Char[] ToCharArray(SqlString p) { return p.IsNull? null: p.Value.ToCharArray(); } | |
536 /// <summary>Converts the value from <c>SqlChars</c> to an equivalent <c>Char[]</c> value.</summary> | |
537 public static Char[] ToCharArray(SqlChars p) { return p.IsNull? null: p.Value; } | |
538 | |
539 #endif | |
540 | |
541 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>Char[]</c> value.</summary> | |
542 public static Char[] ToCharArray(Byte[] p) | |
543 { | |
544 if (p == null) return null; | |
545 | |
546 var chars = new Char[p.Length / sizeof(Char)]; | |
547 | |
548 Buffer.BlockCopy(p, 0, chars, 0, p.Length); | |
549 return chars; | |
550 } | |
551 | |
552 public static Char[] ToCharArray(Binary p) | |
553 { | |
554 if (p == null) return null; | |
555 | |
556 var chars = new Char[p.Length / sizeof(Char)]; | |
557 | |
558 Buffer.BlockCopy(p.ToArray(), 0, chars, 0, p.Length); | |
559 return chars; | |
560 } | |
561 | |
562 /// <summary>Converts the value of a specified object to an equivalent <c>Char[]</c> value.</summary> | |
563 public static Char[] ToCharArray(object p) | |
564 { | |
565 if (p == null || p is DBNull) return null; | |
566 | |
567 if (p is Char[]) return (Char[])p; | |
568 | |
569 // Scalar Types. | |
570 // | |
571 if (p is String) return ToCharArray((String)p); | |
572 | |
573 #if !SILVERLIGHT | |
574 | |
575 // SqlTypes | |
576 // | |
577 if (p is SqlString) return ToCharArray((SqlString)p); | |
578 if (p is SqlChars) return ToCharArray((SqlChars)p); | |
579 | |
580 #endif | |
581 if (p is Byte[]) return ToCharArray((Byte[])p); | |
582 if (p is Binary) return ToCharArray(((Binary)p).ToArray()); | |
583 | |
584 return ToString(p).ToCharArray(); | |
585 } | |
586 | |
587 #endregion | |
588 | |
589 #region XmlReader | |
590 | |
591 #if !SILVERLIGHT | |
592 | |
593 // Scalar Types. | |
594 // | |
595 /// <summary>Converts the value from <c>String</c> to an equivalent <c>XmlReader</c> value.</summary> | |
596 public static XmlReader ToXmlReader(String p) { return p == null? null: new XmlTextReader(new StringReader(p)); } | |
597 | |
598 // SqlTypes | |
599 // | |
600 /// <summary>Converts the value from <c>SqlXml</c> to an equivalent <c>XmlReader</c> value.</summary> | |
601 public static XmlReader ToXmlReader(SqlXml p) { return p.IsNull? null: p.CreateReader(); } | |
602 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>XmlReader</c> value.</summary> | |
603 public static XmlReader ToXmlReader(SqlString p) { return p.IsNull? null: new XmlTextReader(new StringReader(p.Value)); } | |
604 /// <summary>Converts the value from <c>SqlChars</c> to an equivalent <c>XmlReader</c> value.</summary> | |
605 public static XmlReader ToXmlReader(SqlChars p) { return p.IsNull? null: new XmlTextReader(new StringReader(p.ToSqlString().Value)); } | |
606 /// <summary>Converts the value from <c>SqlBinary</c> to an equivalent <c>XmlReader</c> value.</summary> | |
607 public static XmlReader ToXmlReader(SqlBinary p) { return p.IsNull? null: new XmlTextReader(new MemoryStream(p.Value)); } | |
608 | |
609 // Other Types. | |
610 // | |
611 /// <summary>Converts the value from <c>Stream</c> to an equivalent <c>XmlReader</c> value.</summary> | |
612 public static XmlReader ToXmlReader(Stream p) { return p == null? null: new XmlTextReader(p); } | |
613 /// <summary>Converts the value from <c>TextReader</c> to an equivalent <c>XmlReader</c> value.</summary> | |
614 public static XmlReader ToXmlReader(TextReader p) { return p == null? null: new XmlTextReader(p); } | |
615 /// <summary>Converts the value from <c>XmlDocument</c> to an equivalent <c>XmlReader</c> value.</summary> | |
616 public static XmlReader ToXmlReader(XmlDocument p) { return p == null? null: new XmlTextReader(new StringReader(p.InnerXml)); } | |
617 | |
618 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>XmlReader</c> value.</summary> | |
619 public static XmlReader ToXmlReader(Char[] p) { return p == null? null: new XmlTextReader(new StringReader(new string(p))); } | |
620 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>XmlReader</c> value.</summary> | |
621 public static XmlReader ToXmlReader(Byte[] p) { return p == null? null: new XmlTextReader(new MemoryStream(p)); } | |
622 public static XmlReader ToXmlReader(Binary p) { return p == null? null: new XmlTextReader(new MemoryStream(p.ToArray())); } | |
623 | |
624 /// <summary>Converts the value of a specified object to an equivalent <c>XmlReader</c> value.</summary> | |
625 public static XmlReader ToXmlReader(object p) | |
626 { | |
627 if (p == null || p is DBNull) return null; | |
628 | |
629 if (p is XmlReader) return (XmlReader)p; | |
630 | |
631 // Scalar Types. | |
632 // | |
633 if (p is String) return ToXmlReader((String)p); | |
634 | |
635 // SqlTypes | |
636 // | |
637 if (p is SqlXml) return ToXmlReader((SqlXml)p); | |
638 if (p is SqlString) return ToXmlReader((SqlString)p); | |
639 if (p is SqlChars) return ToXmlReader((SqlChars)p); | |
640 if (p is SqlBinary) return ToXmlReader((SqlBinary)p); | |
641 | |
642 // Other Types. | |
643 // | |
644 if (p is XmlDocument) return ToXmlReader((XmlDocument)p); | |
645 | |
646 if (p is Char[]) return ToXmlReader((Char[])p); | |
647 if (p is Byte[]) return ToXmlReader((Byte[])p); | |
648 if (p is Binary) return ToXmlReader(((Binary)p).ToArray()); | |
649 | |
650 throw CreateInvalidCastException(p.GetType(), typeof(XmlReader)); | |
651 } | |
652 | |
653 #endif | |
654 | |
655 #endregion | |
656 | |
657 #region XmlDocument | |
658 | |
659 #if !SILVERLIGHT | |
660 | |
661 // Scalar Types. | |
662 // | |
663 /// <summary>Converts the value from <c>String</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
664 public static XmlDocument ToXmlDocument(String p) | |
665 { | |
666 if (string.IsNullOrEmpty(p)) return null; | |
667 | |
668 var doc = new XmlDocument(); | |
669 | |
670 doc.LoadXml(p); | |
671 | |
672 return doc; | |
673 } | |
674 | |
675 // SqlTypes | |
676 // | |
677 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
678 public static XmlDocument ToXmlDocument(SqlString p) { return p.IsNull? null: ToXmlDocument(p.Value); } | |
679 /// <summary>Converts the value from <c>SqlXml</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
680 public static XmlDocument ToXmlDocument(SqlXml p) { return p.IsNull? null: ToXmlDocument(p.Value); } | |
681 /// <summary>Converts the value from <c>SqlChars</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
682 public static XmlDocument ToXmlDocument(SqlChars p) { return p.IsNull? null: ToXmlDocument(p.ToSqlString().Value); } | |
683 /// <summary>Converts the value from <c>SqlBinary</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
684 public static XmlDocument ToXmlDocument(SqlBinary p) { return p.IsNull? null: ToXmlDocument(new MemoryStream(p.Value)); } | |
685 | |
686 // Other Types. | |
687 // | |
688 /// <summary>Converts the value from <c>Stream</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
689 public static XmlDocument ToXmlDocument(Stream p) | |
690 { | |
691 if (p == null) return null; | |
692 | |
693 var doc = new XmlDocument(); | |
694 | |
695 doc.Load(p); | |
696 | |
697 return doc; | |
698 } | |
699 | |
700 /// <summary>Converts the value from <c>TextReader</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
701 public static XmlDocument ToXmlDocument(TextReader p) | |
702 { | |
703 if (p == null) return null; | |
704 | |
705 var doc = new XmlDocument(); | |
706 | |
707 doc.Load(p); | |
708 | |
709 return doc; | |
710 } | |
711 | |
712 /// <summary>Converts the value from <c>XmlReader</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
713 public static XmlDocument ToXmlDocument(XmlReader p) | |
714 { | |
715 if (p == null) return null; | |
716 | |
717 var doc = new XmlDocument(); | |
718 | |
719 doc.Load(p); | |
720 | |
721 return doc; | |
722 } | |
723 | |
724 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
725 public static XmlDocument ToXmlDocument(Char[] p) { return p == null || p.Length == 0? null: ToXmlDocument(new string(p)); } | |
726 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>XmlDocument</c> value.</summary> | |
727 public static XmlDocument ToXmlDocument(Byte[] p) { return p == null || p.Length == 0? null: ToXmlDocument(new MemoryStream(p)); } | |
728 public static XmlDocument ToXmlDocument(Binary p) { return p == null || p.Length == 0? null: ToXmlDocument(new MemoryStream(p.ToArray())); } | |
729 | |
730 /// <summary>Converts the value of a specified object to an equivalent <c>XmlDocument</c> value.</summary> | |
731 public static XmlDocument ToXmlDocument(object p) | |
732 { | |
733 if (p == null || p is DBNull) return null; | |
734 | |
735 if (p is XmlDocument) return (XmlDocument)p; | |
736 | |
737 // Scalar Types. | |
738 // | |
739 if (p is String) return ToXmlDocument((String)p); | |
740 | |
741 // SqlTypes | |
742 // | |
743 if (p is SqlChars) return ToXmlDocument((SqlChars)p); | |
744 if (p is SqlBinary) return ToXmlDocument((SqlBinary)p); | |
745 | |
746 // Other Types. | |
747 // | |
748 | |
749 if (p is Char[]) return ToXmlDocument((Char[])p); | |
750 if (p is Byte[]) return ToXmlDocument((Byte[])p); | |
751 if (p is Binary) return ToXmlDocument(((Binary)p).ToArray()); | |
752 | |
753 throw CreateInvalidCastException(p.GetType(), typeof(XmlDocument)); | |
754 } | |
755 | |
756 #endif | |
757 | |
758 #endregion | |
759 | |
760 #region XElement | |
761 | |
762 #if !SILVERLIGHT | |
763 | |
764 // Scalar Types. | |
765 // | |
766 /// <summary>Converts the value from <c>String</c> to an equivalent <c>XElement</c> value.</summary> | |
767 public static XElement ToXElement(String p) | |
768 { | |
769 if (string.IsNullOrEmpty(p)) return null; | |
770 | |
771 var doc = XElement.Parse(p); | |
772 | |
773 return doc; | |
774 } | |
775 | |
776 // SqlTypes | |
777 // | |
778 /// <summary>Converts the value from <c>SqlString</c> to an equivalent <c>XElement</c> value.</summary> | |
779 public static XElement ToXElement(SqlString p) { return p.IsNull ? null : ToXElement(p.Value); } | |
780 /// <summary>Converts the value from <c>SqlXml</c> to an equivalent <c>XElement</c> value.</summary> | |
781 public static XElement ToXElement(SqlXml p) { return p.IsNull ? null : ToXElement(p.Value); } | |
782 /// <summary>Converts the value from <c>SqlChars</c> to an equivalent <c>XElement</c> value.</summary> | |
783 public static XElement ToXElement(SqlChars p) { return p.IsNull ? null : ToXElement(p.ToSqlString().Value); } | |
784 /// <summary>Converts the value from <c>SqlBinary</c> to an equivalent <c>XElement</c> value.</summary> | |
785 public static XElement ToXElement(SqlBinary p) { return p.IsNull ? null : ToXElement(new MemoryStream(p.Value)); } | |
786 | |
787 // Other Types. | |
788 // | |
789 /// <summary>Converts the value from <c>Stream</c> to an equivalent <c>XElement</c> value.</summary> | |
790 public static XElement ToXElement(Stream p) | |
791 { | |
792 if (p == null) return null; | |
793 | |
794 using (XmlReader r = XmlReader.Create(p)) | |
795 return XElement.Load(r); | |
796 } | |
797 | |
798 /// <summary>Converts the value from <c>TextReader</c> to an equivalent <c>XElement</c> value.</summary> | |
799 public static XElement ToXElement(TextReader p) | |
800 { | |
801 if (p == null) return null; | |
802 | |
803 var doc = XElement.Load(p); | |
804 | |
805 return doc; | |
806 } | |
807 | |
808 /// <summary>Converts the value from <c>XmlReader</c> to an equivalent <c>XElement</c> value.</summary> | |
809 public static XElement ToXElement(XmlReader p) | |
810 { | |
811 if (p == null) return null; | |
812 | |
813 var doc = XElement.Load(p); | |
814 | |
815 return doc; | |
816 } | |
817 | |
818 /// <summary>Converts the value from <c>Char[]</c> to an equivalent <c>XElement</c> value.</summary> | |
819 public static XElement ToXElement(Char[] p) { return p == null || p.Length == 0 ? null : ToXElement(new string(p)); } | |
820 /// <summary>Converts the value from <c>Byte[]</c> to an equivalent <c>XElement</c> value.</summary> | |
821 public static XElement ToXElement(Byte[] p) { return p == null || p.Length == 0 ? null : ToXElement(new MemoryStream(p)); } | |
822 public static XElement ToXElement(Binary p) { return p == null || p.Length == 0 ? null : ToXElement(new MemoryStream(p.ToArray())); } | |
823 | |
824 /// <summary>Converts the value of a specified object to an equivalent <c>XElement</c> value.</summary> | |
825 public static XElement ToXElement(object p) | |
826 { | |
827 if (p == null || p is DBNull) return null; | |
828 | |
829 if (p is XElement) return (XElement)p; | |
830 | |
831 // Scalar Types. | |
832 // | |
833 if (p is String) return ToXElement((String)p); | |
834 | |
835 // SqlTypes | |
836 // | |
837 if (p is SqlChars) return ToXElement((SqlChars)p); | |
838 if (p is SqlBinary) return ToXElement((SqlBinary)p); | |
839 | |
840 // Other Types. | |
841 // | |
842 | |
843 if (p is Char[]) return ToXElement((Char[])p); | |
844 if (p is Byte[]) return ToXElement((Byte[])p); | |
845 if (p is Binary) return ToXElement(((Binary)p).ToArray()); | |
846 | |
847 throw CreateInvalidCastException(p.GetType(), typeof(XElement)); | |
848 } | |
849 | |
850 #endif | |
851 | |
852 #endregion | |
853 | |
854 #endregion | |
855 | |
856 #region ChangeTypeFromString | |
857 | |
858 public static object ChangeTypeFromString(string str, Type type) | |
859 { | |
860 if (str == null) | |
861 return null; | |
862 | |
863 if (type == typeof(string)) | |
864 return str; | |
865 | |
866 var underlyingType = type; | |
867 var isNullable = false; | |
868 | |
869 if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition() == typeof(Nullable<>)) | |
870 { | |
871 isNullable = true; | |
872 underlyingType = underlyingType.GetGenericArguments()[0]; | |
873 } | |
874 | |
875 if (underlyingType.IsEnum) | |
876 return Enum.Parse(type, str, false); | |
877 | |
878 if (isNullable) | |
879 { | |
880 switch (Type.GetTypeCode(underlyingType)) | |
881 { | |
882 case TypeCode.Boolean : return ToNullableBoolean (str); | |
883 case TypeCode.Char : return ToNullableChar (str); | |
884 case TypeCode.SByte : return ToNullableSByte (str); | |
885 case TypeCode.Byte : return ToNullableByte (str); | |
886 case TypeCode.Int16 : return ToNullableInt16 (str); | |
887 case TypeCode.UInt16 : return ToNullableUInt16 (str); | |
888 case TypeCode.Int32 : return ToNullableInt32 (str); | |
889 case TypeCode.UInt32 : return ToNullableUInt32 (str); | |
890 case TypeCode.Int64 : return ToNullableInt64 (str); | |
891 case TypeCode.UInt64 : return ToNullableUInt64 (str); | |
892 case TypeCode.Single : return ToNullableSingle (str); | |
893 case TypeCode.Double : return ToNullableDouble (str); | |
894 case TypeCode.Decimal : return ToNullableDecimal (str); | |
895 case TypeCode.DateTime : return ToNullableDateTime(str); | |
896 case TypeCode.Object : | |
897 if (type == typeof(Guid)) return ToNullableGuid (str); | |
898 if (type == typeof(DateTimeOffset)) return ToNullableDateTimeOffset(str); | |
899 if (type == typeof(TimeSpan)) return ToNullableTimeSpan (str); | |
900 break; | |
901 default : break; | |
902 } | |
903 } | |
904 else | |
905 { | |
906 switch (Type.GetTypeCode(underlyingType)) | |
907 { | |
908 case TypeCode.Boolean : return ToBoolean(str); | |
909 case TypeCode.Char : return ToChar (str); | |
910 case TypeCode.SByte : return ToSByte (str); | |
911 case TypeCode.Byte : return ToByte (str); | |
912 case TypeCode.Int16 : return ToInt16 (str); | |
913 case TypeCode.UInt16 : return ToUInt16 (str); | |
914 case TypeCode.Int32 : return ToInt32 (str); | |
915 case TypeCode.UInt32 : return ToUInt32 (str); | |
916 case TypeCode.Int64 : return ToInt64 (str); | |
917 case TypeCode.UInt64 : return ToUInt64 (str); | |
918 case TypeCode.Single : return ToSingle (str); | |
919 case TypeCode.Double : return ToDouble (str); | |
920 case TypeCode.Decimal : return ToDecimal (str); | |
921 case TypeCode.DateTime : return ToDateTime(str); | |
922 default : break; | |
923 } | |
924 | |
925 if (type.IsArray) | |
926 { | |
927 if (type == typeof(byte[])) return ToByteArray(str); | |
928 } | |
929 | |
930 if (type.IsClass) | |
931 { | |
932 if (type == typeof(Binary)) return ToLinqBinary (str); | |
933 } | |
934 } | |
935 | |
936 if (type == typeof(Guid)) return ToGuid (str); | |
937 if (type == typeof(DateTimeOffset)) return ToDateTimeOffset(str); | |
938 if (type == typeof(TimeSpan)) return ToTimeSpan (str); | |
939 | |
940 #if !SILVERLIGHT | |
941 | |
942 if (type == typeof(SqlByte)) return ToSqlByte (str); | |
943 if (type == typeof(SqlInt16)) return ToSqlInt16 (str); | |
944 if (type == typeof(SqlInt32)) return ToSqlInt32 (str); | |
945 if (type == typeof(SqlInt64)) return ToSqlInt64 (str); | |
946 if (type == typeof(SqlSingle)) return ToSqlSingle (str); | |
947 if (type == typeof(SqlBoolean)) return ToSqlBoolean (str); | |
948 if (type == typeof(SqlDouble)) return ToSqlDouble (str); | |
949 if (type == typeof(SqlDateTime)) return ToSqlDateTime(str); | |
950 if (type == typeof(SqlDecimal)) return ToSqlDecimal (str); | |
951 if (type == typeof(SqlMoney)) return ToSqlMoney (str); | |
952 if (type == typeof(SqlString)) return ToSqlString (str); | |
953 if (type == typeof(SqlGuid)) return ToSqlGuid (str); | |
954 | |
955 #endif | |
956 | |
957 return System.Convert.ChangeType(str, type, Thread.CurrentThread.CurrentCulture); | |
958 } | |
959 | |
960 | |
961 #endregion | |
962 | |
963 static Exception CreateInvalidCastException(Type originalType, Type conversionType) | |
964 { | |
965 return new InvalidCastException(string.Format(Resources.Convert_InvalidCast, originalType.FullName, conversionType.FullName)); | |
966 } | |
967 } | |
968 } |