Added initial documentation tree using doxygen. More tweaks on the license text ensur...
[lwes-dotnet/github-mirror.git] / Org.Lwes / Coercion.cs
blob9b6ca99d7ce69c323305aa70582e90e78c518cf3
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes
22 using System;
23 using System.Net;
25 /// <summary>
26 /// Utility class for coercing attribute values.
27 /// </summary>
28 public static class Coercion
30 #region Methods
32 /// <summary>
33 /// Tries to coerce an Int16 into a UInt16.
34 /// </summary>
35 /// <param name="input">the input value</param>
36 /// <param name="output">the output value</param>
37 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
38 public static bool TryCoerce(short input, out ushort output)
40 if (input >= 0)
42 output = Convert.ToUInt16(input);
43 return true;
45 output = default(ushort);
46 return false;
49 /// <summary>
50 /// Tries to coerce an Int32 into a UInt16.
51 /// </summary>
52 /// <param name="input">the input value</param>
53 /// <param name="output">the output value</param>
54 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
55 public static bool TryCoerce(int input, out ushort output)
57 if (input >= 0 && input <= ushort.MaxValue)
59 output = Convert.ToUInt16(input);
60 return true;
62 output = default(ushort);
63 return false;
66 /// <summary>
67 /// Tries to coerce an UInt32 into a UInt16.
68 /// </summary>
69 /// <param name="input">the input value</param>
70 /// <param name="output">the output value</param>
71 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
72 public static bool TryCoerce(uint input, out ushort output)
74 if (input <= ushort.MaxValue)
76 output = Convert.ToUInt16(input);
77 return true;
79 output = default(ushort);
80 return false;
83 /// <summary>
84 /// Tries to coerce an Int64 into a UInt16.
85 /// </summary>
86 /// <param name="input">the input value</param>
87 /// <param name="output">the output value</param>
88 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
89 public static bool TryCoerce(long input, out ushort output)
91 if (input >= 0 && input <= ushort.MaxValue)
93 output = Convert.ToUInt16(input);
94 return true;
96 output = default(ushort);
97 return false;
100 /// <summary>
101 /// Tries to coerce an UInt64 into a UInt16.
102 /// </summary>
103 /// <param name="input">the input value</param>
104 /// <param name="output">the output value</param>
105 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
106 public static bool TryCoerce(ulong input, out ushort output)
108 if (input <= ushort.MaxValue)
110 output = Convert.ToUInt16(input);
111 return true;
113 output = default(ushort);
114 return false;
117 /// <summary>
118 /// Tries to coerce an string into a UInt16.
119 /// </summary>
120 /// <param name="input">the input value</param>
121 /// <param name="output">the output value</param>
122 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
123 public static bool TryCoerce(string input, out ushort output)
125 return ushort.TryParse(input, out output);
128 /// <summary>
129 /// Tries to coerce an string into a UInt16.
130 /// </summary>
131 /// <param name="input">the input value</param>
132 /// <param name="output">the output value</param>
133 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
134 public static bool TryCoerce(IPAddress input, out ushort output)
136 output = default(ushort);
137 return false;
140 /// <summary>
141 /// Tries to coerce an bool into a UInt16.
142 /// </summary>
143 /// <param name="input">the input value</param>
144 /// <param name="output">the output value</param>
145 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
146 public static bool TryCoerce(bool input, out ushort output)
148 output = Convert.ToUInt16(input);
149 return true;
152 /// <summary>
153 /// Tries to coerce an UInt16 into a Int16.
154 /// </summary>
155 /// <param name="input">the input value</param>
156 /// <param name="output">the output value</param>
157 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
158 public static bool TryCoerce(ushort input, out short output)
160 if (input < short.MaxValue)
162 output = Convert.ToInt16(input);
163 return true;
165 output = default(short);
166 return false;
169 /// <summary>
170 /// Tries to coerce an Int32 into a Int16.
171 /// </summary>
172 /// <param name="input">the input value</param>
173 /// <param name="output">the output value</param>
174 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
175 public static bool TryCoerce(int input, out short output)
177 if (input >= short.MinValue && input <= short.MaxValue)
179 output = Convert.ToInt16(input);
180 return true;
182 output = default(short);
183 return false;
186 /// <summary>
187 /// Tries to coerce an UInt32 into a Int16.
188 /// </summary>
189 /// <param name="input">the input value</param>
190 /// <param name="output">the output value</param>
191 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
192 public static bool TryCoerce(uint input, out short output)
194 if (input <= short.MaxValue)
196 output = Convert.ToInt16(input);
197 return true;
199 output = default(short);
200 return false;
203 /// <summary>
204 /// Tries to coerce an Int64 into a Int16.
205 /// </summary>
206 /// <param name="input">the input value</param>
207 /// <param name="output">the output value</param>
208 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
209 public static bool TryCoerce(long input, out short output)
211 if (input >= 0 && input <= ushort.MaxValue)
213 output = Convert.ToInt16(input);
214 return true;
216 output = default(short);
217 return false;
220 /// <summary>
221 /// Tries to coerce an UInt64 into a Int16.
222 /// </summary>
223 /// <param name="input">the input value</param>
224 /// <param name="output">the output value</param>
225 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
226 public static bool TryCoerce(ulong input, out short output)
228 if (input <= (ulong)short.MaxValue)
230 output = Convert.ToInt16(input);
231 return true;
233 output = default(short);
234 return false;
237 /// <summary>
238 /// Tries to coerce an string into a Int16.
239 /// </summary>
240 /// <param name="input">the input value</param>
241 /// <param name="output">the output value</param>
242 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
243 public static bool TryCoerce(string input, out short output)
245 return short.TryParse(input, out output);
248 /// <summary>
249 /// Tries to coerce an string into a Int16.
250 /// </summary>
251 /// <param name="input">the input value</param>
252 /// <param name="output">the output value</param>
253 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
254 public static bool TryCoerce(IPAddress input, out short output)
256 output = default(short);
257 return false;
260 /// <summary>
261 /// Tries to coerce an Boolean into a Int16.
262 /// </summary>
263 /// <param name="input">the input value</param>
264 /// <param name="output">the output value</param>
265 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
266 public static bool TryCoerce(bool input, out short output)
268 output = Convert.ToInt16(input);
269 return true;
272 /// <summary>
273 /// Tries to coerce an Int16 into a UInt32.
274 /// </summary>
275 /// <param name="input">the input value</param>
276 /// <param name="output">the output value</param>
277 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
278 public static bool TryCoerce(short input, out uint output)
280 if (input >= 0)
282 output = Convert.ToUInt32(input);
283 return true;
285 output = default(uint);
286 return false;
289 /// <summary>
290 /// Tries to coerce an UInt16 into a UInt32.
291 /// </summary>
292 /// <param name="input">the input value</param>
293 /// <param name="output">the output value</param>
294 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
295 public static bool TryCoerce(ushort input, out uint output)
297 output = Convert.ToUInt32(input);
298 return true;
301 /// <summary>
302 /// Tries to coerce an Int32 into a UInt32.
303 /// </summary>
304 /// <param name="input">the input value</param>
305 /// <param name="output">the output value</param>
306 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
307 public static bool TryCoerce(int input, out uint output)
309 if (input >= 0)
311 output = Convert.ToUInt32(input);
312 return true;
314 output = default(uint);
315 return false;
318 /// <summary>
319 /// Tries to coerce an Int64 into a UInt32.
320 /// </summary>
321 /// <param name="input">the input value</param>
322 /// <param name="output">the output value</param>
323 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
324 public static bool TryCoerce(long input, out uint output)
326 if (input >= 0 && input <= uint.MaxValue)
328 output = Convert.ToUInt32(input);
329 return true;
331 output = default(uint);
332 return false;
335 /// <summary>
336 /// Tries to coerce an UInt64 into a UInt32.
337 /// </summary>
338 /// <param name="input">the input value</param>
339 /// <param name="output">the output value</param>
340 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
341 public static bool TryCoerce(ulong input, out uint output)
343 if (input <= uint.MaxValue)
345 output = Convert.ToUInt32(input);
346 return true;
348 output = default(uint);
349 return false;
352 /// <summary>
353 /// Tries to coerce an string into a UInt32.
354 /// </summary>
355 /// <param name="input">the input value</param>
356 /// <param name="output">the output value</param>
357 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
358 public static bool TryCoerce(string input, out uint output)
360 return uint.TryParse(input, out output);
363 /// <summary>
364 /// Tries to coerce an IPAddress into a UInt32.
365 /// </summary>
366 /// <param name="input">the input value</param>
367 /// <param name="output">the output value</param>
368 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
369 public static bool TryCoerce(IPAddress input, out uint output)
371 byte[] addr = input.GetAddressBytes();
372 output = BitConverter.ToUInt32(addr, 0);
373 return true;
376 /// <summary>
377 /// Tries to coerce an Boolean into a UInt32.
378 /// </summary>
379 /// <param name="input">the input value</param>
380 /// <param name="output">the output value</param>
381 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
382 public static bool TryCoerce(bool input, out uint output)
384 output = Convert.ToUInt32(input);
385 return true;
388 /// <summary>
389 /// Tries to coerce an Int16 into a Int32.
390 /// </summary>
391 /// <param name="input">the input value</param>
392 /// <param name="output">the output value</param>
393 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
394 public static bool TryCoerce(short input, out int output)
396 output = Convert.ToInt32(input);
397 return true;
400 /// <summary>
401 /// Tries to coerce an UInt16 into a Int32.
402 /// </summary>
403 /// <param name="input">the input value</param>
404 /// <param name="output">the output value</param>
405 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
406 public static bool TryCoerce(ushort input, out int output)
408 output = Convert.ToInt32(input);
409 return true;
412 /// <summary>
413 /// Tries to coerce an UInt32 into a Int32.
414 /// </summary>
415 /// <param name="input">the input value</param>
416 /// <param name="output">the output value</param>
417 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
418 public static bool TryCoerce(uint input, out int output)
420 if (input <= int.MaxValue)
422 output = Convert.ToInt32(input);
423 return true;
425 output = default(int);
426 return false;
429 /// <summary>
430 /// Tries to coerce an Int64 into a Int32.
431 /// </summary>
432 /// <param name="input">the input value</param>
433 /// <param name="output">the output value</param>
434 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
435 public static bool TryCoerce(long input, out int output)
437 if (input >= int.MinValue && input <= int.MaxValue)
439 output = Convert.ToInt32(input);
440 return true;
442 output = default(int);
443 return false;
446 /// <summary>
447 /// Tries to coerce an UInt64 into a Int32.
448 /// </summary>
449 /// <param name="input">the input value</param>
450 /// <param name="output">the output value</param>
451 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
452 public static bool TryCoerce(ulong input, out int output)
454 if (input <= int.MaxValue)
456 output = Convert.ToInt32(input);
457 return true;
459 output = default(int);
460 return false;
463 /// <summary>
464 /// Tries to coerce a string into a Int32.
465 /// </summary>
466 /// <param name="input">the input value</param>
467 /// <param name="output">the output value</param>
468 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
469 public static bool TryCoerce(string input, out int output)
471 return int.TryParse(input, out output);
474 /// <summary>
475 /// Tries to coerce an IPAddress into a Int32.
476 /// </summary>
477 /// <param name="input">the input value</param>
478 /// <param name="output">the output value</param>
479 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
480 public static bool TryCoerce(IPAddress input, out int output)
482 if (input == null)
484 output = default(int);
485 return false;
487 byte[] addr = input.GetAddressBytes();
488 output = BitConverter.ToInt32(addr, 0);
489 return true;
492 /// <summary>
493 /// Tries to coerce an Int16 into a Int32.
494 /// </summary>
495 /// <param name="input">the input value</param>
496 /// <param name="output">the output value</param>
497 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
498 public static bool TryCoerce(bool input, out int output)
500 output = Convert.ToInt32(input);
501 return true;
504 /// <summary>
505 /// Tries to coerce an Int16 into a string.
506 /// </summary>
507 /// <param name="input">the input value</param>
508 /// <param name="output">the output value</param>
509 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
510 public static bool TryCoerce(short input, out string output)
512 output = Convert.ToString(input);
513 return true;
516 /// <summary>
517 /// Tries to coerce an UInt16 into a string.
518 /// </summary>
519 /// <param name="input">the input value</param>
520 /// <param name="output">the output value</param>
521 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
522 public static bool TryCoerce(ushort input, out string output)
524 output = Convert.ToString(input);
525 return true;
528 /// <summary>
529 /// Tries to coerce an UInt32 into a string.
530 /// </summary>
531 /// <param name="input">the input value</param>
532 /// <param name="output">the output value</param>
533 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
534 public static bool TryCoerce(uint input, out string output)
536 output = Convert.ToString(input);
537 return true;
540 /// <summary>
541 /// Tries to coerce an Int32 into a string.
542 /// </summary>
543 /// <param name="input">the input value</param>
544 /// <param name="output">the output value</param>
545 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
546 public static bool TryCoerce(int input, out string output)
548 output = Convert.ToString(input);
549 return true;
552 /// <summary>
553 /// Tries to coerce an Int64 into a string.
554 /// </summary>
555 /// <param name="input">the input value</param>
556 /// <param name="output">the output value</param>
557 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
558 public static bool TryCoerce(long input, out string output)
560 output = Convert.ToString(input);
561 return true;
564 /// <summary>
565 /// Tries to coerce an UInt64 into a string.
566 /// </summary>
567 /// <param name="input">the input value</param>
568 /// <param name="output">the output value</param>
569 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
570 public static bool TryCoerce(ulong input, out string output)
572 output = Convert.ToString(input);
573 return true;
576 /// <summary>
577 /// Tries to coerce an IPAddress into a string.
578 /// </summary>
579 /// <param name="input">the input value</param>
580 /// <param name="output">the output value</param>
581 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
582 public static bool TryCoerce(IPAddress input, out string output)
584 output = Convert.ToString(input);
585 return true;
588 /// <summary>
589 /// Tries to coerce an Boolean into a string.
590 /// </summary>
591 /// <param name="input">the input value</param>
592 /// <param name="output">the output value</param>
593 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
594 public static bool TryCoerce(bool input, out string output)
596 output = Convert.ToString(input);
597 return true;
600 /// <summary>
601 /// Tries to coerce an Int16 into an IPAddress.
602 /// </summary>
603 /// <param name="input">the input value</param>
604 /// <param name="output">the output value</param>
605 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
606 public static bool TryCoerce(short input, out IPAddress output)
608 output = default(IPAddress);
609 return true;
612 /// <summary>
613 /// Tries to coerce an UInt16 into an IPAddress.
614 /// </summary>
615 /// <param name="input">the input value</param>
616 /// <param name="output">the output value</param>
617 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
618 public static bool TryCoerce(ushort input, out IPAddress output)
620 output = default(IPAddress);
621 return true;
624 /// <summary>
625 /// Tries to coerce an UInt32 into an IPAddress.
626 /// </summary>
627 /// <param name="input">the input value</param>
628 /// <param name="output">the output value</param>
629 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
630 public static bool TryCoerce(uint input, out IPAddress output)
632 output = new IPAddress(BitConverter.GetBytes(input));
633 return true;
636 /// <summary>
637 /// Tries to coerce an Int32 into an IPAddress.
638 /// </summary>
639 /// <param name="input">the input value</param>
640 /// <param name="output">the output value</param>
641 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
642 public static bool TryCoerce(int input, out IPAddress output)
644 output = new IPAddress(BitConverter.GetBytes(input));
645 return true;
648 /// <summary>
649 /// Tries to coerce an string into an IPAddress.
650 /// </summary>
651 /// <param name="input">the input value</param>
652 /// <param name="output">the output value</param>
653 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
654 public static bool TryCoerce(string input, out IPAddress output)
656 return IPAddress.TryParse(input, out output);
659 /// <summary>
660 /// Tries to coerce an Int64 into an IPAddress.
661 /// </summary>
662 /// <param name="input">the input value</param>
663 /// <param name="output">the output value</param>
664 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
665 public static bool TryCoerce(long input, out IPAddress output)
667 output = default(IPAddress);
668 return true;
671 /// <summary>
672 /// Tries to coerce an UInt64 into an IPAddress.
673 /// </summary>
674 /// <param name="input">the input value</param>
675 /// <param name="output">the output value</param>
676 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
677 public static bool TryCoerce(ulong input, out IPAddress output)
679 output = default(IPAddress);
680 return true;
683 /// <summary>
684 /// Tries to coerce an Boolean into an IPAddress.
685 /// </summary>
686 /// <param name="input">the input value</param>
687 /// <param name="output">the output value</param>
688 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
689 public static bool TryCoerce(bool input, out IPAddress output)
691 output = default(IPAddress);
692 return true;
695 /// <summary>
696 /// Tries to coerce an Int16 into an UInt64.
697 /// </summary>
698 /// <param name="input">the input value</param>
699 /// <param name="output">the output value</param>
700 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
701 public static bool TryCoerce(short input, out long output)
703 output = Convert.ToInt64(input);
704 return true;
707 /// <summary>
708 /// Tries to coerce an UInt16 into an Int64.
709 /// </summary>
710 /// <param name="input">the input value</param>
711 /// <param name="output">the output value</param>
712 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
713 public static bool TryCoerce(ushort input, out long output)
715 output = Convert.ToInt64(input);
716 return true;
719 /// <summary>
720 /// Tries to coerce an UInt32 into an Int64.
721 /// </summary>
722 /// <param name="input">the input value</param>
723 /// <param name="output">the output value</param>
724 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
725 public static bool TryCoerce(uint input, out long output)
727 output = Convert.ToInt64(input);
728 return true;
731 /// <summary>
732 /// Tries to coerce an Int32 into an Int64.
733 /// </summary>
734 /// <param name="input">the input value</param>
735 /// <param name="output">the output value</param>
736 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
737 public static bool TryCoerce(int input, out long output)
739 output = Convert.ToInt64(input);
740 return true;
743 /// <summary>
744 /// Tries to coerce an UInt64 into an Int64.
745 /// </summary>
746 /// <param name="input">the input value</param>
747 /// <param name="output">the output value</param>
748 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
749 public static bool TryCoerce(ulong input, out long output)
751 if (input <= long.MaxValue)
753 output = Convert.ToInt64(input);
754 return true;
756 output = default(long);
757 return false;
760 /// <summary>
761 /// Tries to coerce an string into an Int64.
762 /// </summary>
763 /// <param name="input">the input value</param>
764 /// <param name="output">the output value</param>
765 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
766 public static bool TryCoerce(string input, out long output)
768 return long.TryParse(input, out output);
771 /// <summary>
772 /// Tries to coerce an IPAddress into an Int64.
773 /// </summary>
774 /// <param name="input">the input value</param>
775 /// <param name="output">the output value</param>
776 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
777 public static bool TryCoerce(IPAddress input, out long output)
779 output = default(long);
780 return false;
783 /// <summary>
784 /// Tries to coerce an Boolean into an Int64.
785 /// </summary>
786 /// <param name="input">the input value</param>
787 /// <param name="output">the output value</param>
788 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
789 public static bool TryCoerce(bool input, out long output)
791 output = Convert.ToInt64(input);
792 return false;
795 /// <summary>
796 /// Tries to coerce an Int16 into an UInt64.
797 /// </summary>
798 /// <param name="input">the input value</param>
799 /// <param name="output">the output value</param>
800 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
801 public static bool TryCoerce(short input, out ulong output)
803 output = Convert.ToUInt64(input);
804 return true;
807 /// <summary>
808 /// Tries to coerce an UInt16 into an UInt64.
809 /// </summary>
810 /// <param name="input">the input value</param>
811 /// <param name="output">the output value</param>
812 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
813 public static bool TryCoerce(ushort input, out ulong output)
815 output = Convert.ToUInt64(input);
816 return true;
819 /// <summary>
820 /// Tries to coerce an UInt32 into an UInt64.
821 /// </summary>
822 /// <param name="input">the input value</param>
823 /// <param name="output">the output value</param>
824 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
825 public static bool TryCoerce(uint input, out ulong output)
827 output = Convert.ToUInt64(input);
828 return true;
831 /// <summary>
832 /// Tries to coerce an Int16 into an UInt64.
833 /// </summary>
834 /// <param name="input">the input value</param>
835 /// <param name="output">the output value</param>
836 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
837 public static bool TryCoerce(long input, out ulong output)
839 if (input > 0)
841 output = Convert.ToUInt64(input);
842 return true;
844 output = default(ulong);
845 return false;
848 /// <summary>
849 /// Tries to coerce an string into an UInt64.
850 /// </summary>
851 /// <param name="input">the input value</param>
852 /// <param name="output">the output value</param>
853 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
854 public static bool TryCoerce(string input, out ulong output)
856 return ulong.TryParse(input, out output);
859 /// <summary>
860 /// Tries to coerce an IPAddress into an UInt64.
861 /// </summary>
862 /// <param name="input">the input value</param>
863 /// <param name="output">the output value</param>
864 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
865 public static bool TryCoerce(IPAddress input, out ulong output)
867 output = default(ulong);
868 return false;
871 /// <summary>
872 /// Tries to coerce an Boolean into an UInt64.
873 /// </summary>
874 /// <param name="input">the input value</param>
875 /// <param name="output">the output value</param>
876 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
877 public static bool TryCoerce(bool input, out ulong output)
879 output = Convert.ToUInt64(input);
880 return false;
883 /// <summary>
884 /// Tries to coerce an Int16 into an Boolean.
885 /// </summary>
886 /// <param name="input">the input value</param>
887 /// <param name="output">the output value</param>
888 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
889 public static bool TryCoerce(short input, out bool output)
891 output = Convert.ToBoolean(input);
892 return true;
895 /// <summary>
896 /// Tries to coerce an UInt16 into an Boolean.
897 /// </summary>
898 /// <param name="input">the input value</param>
899 /// <param name="output">the output value</param>
900 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
901 public static bool TryCoerce(ushort input, out bool output)
903 output = Convert.ToBoolean(input);
904 return true;
907 /// <summary>
908 /// Tries to coerce an UInt16 into an Boolean.
909 /// </summary>
910 /// <param name="input">the input value</param>
911 /// <param name="output">the output value</param>
912 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
913 public static bool TryCoerce(uint input, out bool output)
915 output = Convert.ToBoolean(input);
916 return true;
919 /// <summary>
920 /// Tries to coerce an Int64 into an Boolean.
921 /// </summary>
922 /// <param name="input">the input value</param>
923 /// <param name="output">the output value</param>
924 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
925 public static bool TryCoerce(long input, out bool output)
927 output = Convert.ToBoolean(input);
928 return true;
931 /// <summary>
932 /// Tries to coerce an string into an Boolean.
933 /// </summary>
934 /// <param name="input">the input value</param>
935 /// <param name="output">the output value</param>
936 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
937 public static bool TryCoerce(string input, out bool output)
939 return bool.TryParse(input, out output);
942 /// <summary>
943 /// Tries to coerce an IPAddress into an Boolean.
944 /// </summary>
945 /// <param name="input">the input value</param>
946 /// <param name="output">the output value</param>
947 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
948 public static bool TryCoerce(IPAddress input, out bool output)
950 output = default(bool);
951 return false;
954 /// <summary>
955 /// Tries to coerce an UInt64 into an Boolean.
956 /// </summary>
957 /// <param name="input">the input value</param>
958 /// <param name="output">the output value</param>
959 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
960 public static bool TryCoerce(ulong input, out bool output)
962 output = Convert.ToBoolean(input);
963 return true;
966 /// <summary>
967 /// Tries to coerce an input value into a UInt16.
968 /// </summary>
969 /// <typeparam name="V">input value type V</typeparam>
970 /// <param name="tt">TypeToken of the input value</param>
971 /// <param name="input">input value</param>
972 /// <param name="output">reference to a variable to hold the output</param>
973 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
974 internal static bool TryCoerce<V>(TypeToken tt, V input, out ushort output)
976 switch (tt)
978 case TypeToken.UINT16:
979 output = Convert.ToUInt16(input);
980 return true;
981 case TypeToken.INT16:
982 return TryCoerce(Convert.ToInt16(input), out output);
983 case TypeToken.UINT32:
984 return TryCoerce(Convert.ToUInt32(input), out output);
985 case TypeToken.INT32:
986 return TryCoerce(Convert.ToInt32(input), out output);
987 case TypeToken.STRING:
988 return TryCoerce(Convert.ToString(input), out output);
989 case TypeToken.IP_ADDR:
990 return TryCoerce(input as IPAddress, out output);
991 case TypeToken.INT64:
992 return TryCoerce(Convert.ToInt64(input), out output);
993 case TypeToken.UINT64:
994 return TryCoerce(Convert.ToUInt64(input), out output);
995 case TypeToken.BOOLEAN:
996 return TryCoerce(Convert.ToBoolean(input), out output);
998 output = default(ushort);
999 return false;
1002 /// <summary>
1003 /// Tries to coerce an input value into a Int16.
1004 /// </summary>
1005 /// <typeparam name="V">input value type V</typeparam>
1006 /// <param name="tt">TypeToken of the input value</param>
1007 /// <param name="input">input value</param>
1008 /// <param name="output">reference to a variable to hold the output</param>
1009 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1010 internal static bool TryCoerce<V>(TypeToken tt, V input, out short output)
1012 switch (tt)
1014 case TypeToken.UINT16:
1015 return TryCoerce(Convert.ToUInt16(input), out output);
1016 case TypeToken.INT16:
1017 output = Convert.ToInt16(input);
1018 return true;
1019 case TypeToken.UINT32:
1020 return TryCoerce(Convert.ToUInt32(input), out output);
1021 case TypeToken.INT32:
1022 return TryCoerce(Convert.ToInt32(input), out output);
1023 case TypeToken.STRING:
1024 return TryCoerce(Convert.ToString(input), out output);
1025 case TypeToken.IP_ADDR:
1026 return TryCoerce(input as IPAddress, out output);
1027 case TypeToken.INT64:
1028 return TryCoerce(Convert.ToInt64(input), out output);
1029 case TypeToken.UINT64:
1030 return TryCoerce(Convert.ToUInt64(input), out output);
1031 case TypeToken.BOOLEAN:
1032 return TryCoerce(Convert.ToBoolean(input), out output);
1034 output = default(short);
1035 return false;
1038 /// <summary>
1039 /// Tries to coerce an input value into a Int32.
1040 /// </summary>
1041 /// <typeparam name="V">input value type V</typeparam>
1042 /// <param name="tt">TypeToken of the input value</param>
1043 /// <param name="input">input value</param>
1044 /// <param name="output">reference to a variable to hold the output</param>
1045 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1046 internal static bool TryCoerce<V>(TypeToken tt, V input, out uint output)
1048 switch (tt)
1050 case TypeToken.UINT16:
1051 output = Convert.ToUInt16(input);
1052 return true;
1053 case TypeToken.INT16:
1054 return TryCoerce(Convert.ToInt16(input), out output);
1055 case TypeToken.UINT32:
1056 output = Convert.ToUInt32(input);
1057 return true;
1058 case TypeToken.INT32:
1059 return TryCoerce(Convert.ToInt32(input), out output);
1060 case TypeToken.STRING:
1061 return TryCoerce(Convert.ToString(input), out output);
1062 case TypeToken.IP_ADDR:
1063 return TryCoerce(input as IPAddress, out output);
1064 case TypeToken.INT64:
1065 return TryCoerce(Convert.ToInt64(input), out output);
1066 case TypeToken.UINT64:
1067 return TryCoerce(Convert.ToUInt64(input), out output);
1068 case TypeToken.BOOLEAN:
1069 return TryCoerce(Convert.ToBoolean(input), out output);
1071 output = default(ushort);
1072 return false;
1075 /// <summary>
1076 /// Tries to coerce an input value into a Int32.
1077 /// </summary>
1078 /// <typeparam name="V">input value type V</typeparam>
1079 /// <param name="tt">TypeToken of the input value</param>
1080 /// <param name="input">input value</param>
1081 /// <param name="output">reference to a variable to hold the output</param>
1082 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1083 internal static bool TryCoerce<V>(TypeToken tt, V input, out int output)
1085 switch (tt)
1087 case TypeToken.UINT16:
1088 return TryCoerce(Convert.ToUInt16(input), out output);
1089 case TypeToken.INT16:
1090 return TryCoerce(Convert.ToInt16(input), out output);
1091 case TypeToken.UINT32:
1092 return TryCoerce(Convert.ToUInt32(input), out output);
1093 case TypeToken.INT32:
1094 output = Convert.ToInt32(input);
1095 return true;
1096 case TypeToken.STRING:
1097 return TryCoerce(Convert.ToString(input), out output);
1098 case TypeToken.IP_ADDR:
1099 return TryCoerce(input as IPAddress, out output);
1100 case TypeToken.INT64:
1101 return TryCoerce(Convert.ToInt64(input), out output);
1102 case TypeToken.UINT64:
1103 return TryCoerce(Convert.ToUInt64(input), out output);
1104 case TypeToken.BOOLEAN:
1105 return TryCoerce(Convert.ToBoolean(input), out output);
1107 output = default(ushort);
1108 return false;
1111 /// <summary>
1112 /// Tries to coerce an input value into a String.
1113 /// </summary>
1114 /// <typeparam name="V">input value type V</typeparam>
1115 /// <param name="tt">TypeToken of the input value</param>
1116 /// <param name="input">input value</param>
1117 /// <param name="output">reference to a variable to hold the output</param>
1118 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1119 internal static bool TryCoerce<V>(TypeToken tt, V input, out string output)
1121 switch (tt)
1123 case TypeToken.UINT16:
1124 return TryCoerce(Convert.ToUInt16(input), out output);
1125 case TypeToken.INT16:
1126 return TryCoerce(Convert.ToInt16(input), out output);
1127 case TypeToken.UINT32:
1128 return TryCoerce(Convert.ToUInt32(input), out output);
1129 case TypeToken.INT32:
1130 return TryCoerce(Convert.ToInt32(input), out output);
1131 case TypeToken.STRING:
1132 output = Convert.ToString(input);
1133 return true;
1134 case TypeToken.IP_ADDR:
1135 return TryCoerce(input as IPAddress, out output);
1136 case TypeToken.INT64:
1137 return TryCoerce(Convert.ToInt64(input), out output);
1138 case TypeToken.UINT64:
1139 return TryCoerce(Convert.ToUInt64(input), out output);
1140 case TypeToken.BOOLEAN:
1141 return TryCoerce(Convert.ToBoolean(input), out output);
1143 output = default(string);
1144 return false;
1147 /// <summary>
1148 /// Tries to coerce an input value into a IPAddress.
1149 /// </summary>
1150 /// <typeparam name="V">input value type V</typeparam>
1151 /// <param name="tt">TypeToken of the input value</param>
1152 /// <param name="input">input value</param>
1153 /// <param name="output">reference to a variable to hold the output</param>
1154 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1155 internal static bool TryCoerce<V>(TypeToken tt, V input, out IPAddress output)
1157 switch (tt)
1159 case TypeToken.UINT16:
1160 return TryCoerce(Convert.ToUInt16(input), out output);
1161 case TypeToken.INT16:
1162 return TryCoerce(Convert.ToInt16(input), out output);
1163 case TypeToken.UINT32:
1164 return TryCoerce(Convert.ToUInt32(input), out output);
1165 case TypeToken.INT32:
1166 return TryCoerce(Convert.ToInt32(input), out output);
1167 case TypeToken.STRING:
1168 return TryCoerce(Convert.ToString(input), out output);
1169 case TypeToken.IP_ADDR:
1170 output = input as IPAddress;
1171 return true;
1172 case TypeToken.INT64:
1173 return TryCoerce(Convert.ToInt64(input), out output);
1174 case TypeToken.UINT64:
1175 return TryCoerce(Convert.ToUInt64(input), out output);
1176 case TypeToken.BOOLEAN:
1177 return TryCoerce(Convert.ToBoolean(input), out output);
1179 output = default(IPAddress);
1180 return false;
1183 /// <summary>
1184 /// Tries to coerce an input value into a Int64.
1185 /// </summary>
1186 /// <typeparam name="V">input value type V</typeparam>
1187 /// <param name="tt">TypeToken of the input value</param>
1188 /// <param name="input">input value</param>
1189 /// <param name="output">reference to a variable to hold the output</param>
1190 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1191 internal static bool TryCoerce<V>(TypeToken tt, V input, out long output)
1193 switch (tt)
1195 case TypeToken.UINT16:
1196 return TryCoerce(Convert.ToUInt16(input), out output);
1197 case TypeToken.INT16:
1198 return TryCoerce(Convert.ToInt16(input), out output);
1199 case TypeToken.UINT32:
1200 return TryCoerce(Convert.ToUInt32(input), out output);
1201 case TypeToken.INT32:
1202 return TryCoerce(Convert.ToInt32(input), out output);
1203 case TypeToken.STRING:
1204 return TryCoerce(Convert.ToString(input), out output);
1205 case TypeToken.IP_ADDR:
1206 return TryCoerce(input as IPAddress, out output);
1207 case TypeToken.INT64:
1208 output = Convert.ToUInt16(input);
1209 return true;
1210 case TypeToken.UINT64:
1211 return TryCoerce(Convert.ToUInt64(input), out output);
1212 case TypeToken.BOOLEAN:
1213 return TryCoerce(Convert.ToBoolean(input), out output);
1215 output = default(ushort);
1216 return false;
1219 /// <summary>
1220 /// Tries to coerce an input value into a UInt64.
1221 /// </summary>
1222 /// <typeparam name="V">input value type V</typeparam>
1223 /// <param name="tt">TypeToken of the input value</param>
1224 /// <param name="input">input value</param>
1225 /// <param name="output">reference to a variable to hold the output</param>
1226 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1227 internal static bool TryCoerce<V>(TypeToken tt, V input, out ulong output)
1229 switch (tt)
1231 case TypeToken.UINT16:
1232 return TryCoerce(Convert.ToUInt16(input), out output);
1233 case TypeToken.INT16:
1234 return TryCoerce(Convert.ToInt16(input), out output);
1235 case TypeToken.UINT32:
1236 return TryCoerce(Convert.ToUInt32(input), out output);
1237 case TypeToken.INT32:
1238 return TryCoerce(Convert.ToInt32(input), out output);
1239 case TypeToken.STRING:
1240 return TryCoerce(Convert.ToString(input), out output);
1241 case TypeToken.IP_ADDR:
1242 return TryCoerce(input as IPAddress, out output);
1243 case TypeToken.INT64:
1244 return TryCoerce(Convert.ToInt64(input), out output);
1245 case TypeToken.UINT64:
1246 output = Convert.ToUInt64(input);
1247 return true;
1248 case TypeToken.BOOLEAN:
1249 return TryCoerce(Convert.ToBoolean(input), out output);
1251 output = default(ulong);
1252 return false;
1255 /// <summary>
1256 /// Tries to coerce an input value into a Boolean.
1257 /// </summary>
1258 /// <typeparam name="V">input value type V</typeparam>
1259 /// <param name="tt">TypeToken of the input value</param>
1260 /// <param name="input">input value</param>
1261 /// <param name="output">reference to a variable to hold the output</param>
1262 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1263 internal static bool TryCoerce<V>(TypeToken tt, V input, out bool output)
1265 switch (tt)
1267 case TypeToken.UINT16:
1268 return TryCoerce(Convert.ToUInt16(input), out output);
1269 case TypeToken.INT16:
1270 return TryCoerce(Convert.ToInt16(input), out output);
1271 case TypeToken.UINT32:
1272 return TryCoerce(Convert.ToUInt32(input), out output);
1273 case TypeToken.INT32:
1274 return TryCoerce(Convert.ToInt32(input), out output);
1275 case TypeToken.STRING:
1276 return TryCoerce(Convert.ToString(input), out output);
1277 case TypeToken.IP_ADDR:
1278 return TryCoerce(input as IPAddress, out output);
1279 case TypeToken.INT64:
1280 return TryCoerce(Convert.ToInt64(input), out output);
1281 case TypeToken.UINT64:
1282 return TryCoerce(Convert.ToUInt64(input), out output);
1283 case TypeToken.BOOLEAN:
1284 output = Convert.ToBoolean(input);
1285 return true;
1287 output = default(bool);
1288 return false;
1291 #endregion Methods