Initial check in of the LWES DotNet Binding - this is ALPHA code and NOT yet stable.
[lwes-dotnet/github-mirror.git] / Org.Lwes / Coercion.cs
blob42266cd58f8501a1505bc0482516c495222d0675
1 namespace Org.Lwes
3 using System;
4 using System.Net;
6 /// <summary>
7 /// Utility class for coercing attribute values.
8 /// </summary>
9 public static class Coercion
11 #region Methods
13 /// <summary>
14 /// Tries to coerce an Int16 into a UInt16.
15 /// </summary>
16 /// <param name="input">the input value</param>
17 /// <param name="output">the output value</param>
18 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
19 public static bool TryCoerce(short input, out ushort output)
21 if (input >= 0)
23 output = Convert.ToUInt16(input);
24 return true;
26 output = default(ushort);
27 return false;
30 /// <summary>
31 /// Tries to coerce an Int32 into a UInt16.
32 /// </summary>
33 /// <param name="input">the input value</param>
34 /// <param name="output">the output value</param>
35 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
36 public static bool TryCoerce(int input, out ushort output)
38 if (input >= 0 && input <= ushort.MaxValue)
40 output = Convert.ToUInt16(input);
41 return true;
43 output = default(ushort);
44 return false;
47 /// <summary>
48 /// Tries to coerce an UInt32 into a UInt16.
49 /// </summary>
50 /// <param name="input">the input value</param>
51 /// <param name="output">the output value</param>
52 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
53 public static bool TryCoerce(uint input, out ushort output)
55 if (input <= ushort.MaxValue)
57 output = Convert.ToUInt16(input);
58 return true;
60 output = default(ushort);
61 return false;
64 /// <summary>
65 /// Tries to coerce an Int64 into a UInt16.
66 /// </summary>
67 /// <param name="input">the input value</param>
68 /// <param name="output">the output value</param>
69 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
70 public static bool TryCoerce(long input, out ushort output)
72 if (input >= 0 && input <= ushort.MaxValue)
74 output = Convert.ToUInt16(input);
75 return true;
77 output = default(ushort);
78 return false;
81 /// <summary>
82 /// Tries to coerce an UInt64 into a UInt16.
83 /// </summary>
84 /// <param name="input">the input value</param>
85 /// <param name="output">the output value</param>
86 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
87 public static bool TryCoerce(ulong input, out ushort output)
89 if (input <= ushort.MaxValue)
91 output = Convert.ToUInt16(input);
92 return true;
94 output = default(ushort);
95 return false;
98 /// <summary>
99 /// Tries to coerce an string into a UInt16.
100 /// </summary>
101 /// <param name="input">the input value</param>
102 /// <param name="output">the output value</param>
103 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
104 public static bool TryCoerce(string input, out ushort output)
106 return ushort.TryParse(input, out output);
109 /// <summary>
110 /// Tries to coerce an string into a UInt16.
111 /// </summary>
112 /// <param name="input">the input value</param>
113 /// <param name="output">the output value</param>
114 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
115 public static bool TryCoerce(IPAddress input, out ushort output)
117 output = default(ushort);
118 return false;
121 /// <summary>
122 /// Tries to coerce an bool into a UInt16.
123 /// </summary>
124 /// <param name="input">the input value</param>
125 /// <param name="output">the output value</param>
126 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
127 public static bool TryCoerce(bool input, out ushort output)
129 output = Convert.ToUInt16(input);
130 return true;
133 /// <summary>
134 /// Tries to coerce an UInt16 into a Int16.
135 /// </summary>
136 /// <param name="input">the input value</param>
137 /// <param name="output">the output value</param>
138 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
139 public static bool TryCoerce(ushort input, out short output)
141 if (input < short.MaxValue)
143 output = Convert.ToInt16(input);
144 return true;
146 output = default(short);
147 return false;
150 /// <summary>
151 /// Tries to coerce an Int32 into a Int16.
152 /// </summary>
153 /// <param name="input">the input value</param>
154 /// <param name="output">the output value</param>
155 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
156 public static bool TryCoerce(int input, out short output)
158 if (input >= short.MinValue && input <= short.MaxValue)
160 output = Convert.ToInt16(input);
161 return true;
163 output = default(short);
164 return false;
167 /// <summary>
168 /// Tries to coerce an UInt32 into a Int16.
169 /// </summary>
170 /// <param name="input">the input value</param>
171 /// <param name="output">the output value</param>
172 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
173 public static bool TryCoerce(uint input, out short output)
175 if (input <= short.MaxValue)
177 output = Convert.ToInt16(input);
178 return true;
180 output = default(short);
181 return false;
184 /// <summary>
185 /// Tries to coerce an Int64 into a Int16.
186 /// </summary>
187 /// <param name="input">the input value</param>
188 /// <param name="output">the output value</param>
189 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
190 public static bool TryCoerce(long input, out short output)
192 if (input >= 0 && input <= ushort.MaxValue)
194 output = Convert.ToInt16(input);
195 return true;
197 output = default(short);
198 return false;
201 /// <summary>
202 /// Tries to coerce an UInt64 into a Int16.
203 /// </summary>
204 /// <param name="input">the input value</param>
205 /// <param name="output">the output value</param>
206 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
207 public static bool TryCoerce(ulong input, out short output)
209 if (input <= (ulong)short.MaxValue)
211 output = Convert.ToInt16(input);
212 return true;
214 output = default(short);
215 return false;
218 /// <summary>
219 /// Tries to coerce an string into a Int16.
220 /// </summary>
221 /// <param name="input">the input value</param>
222 /// <param name="output">the output value</param>
223 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
224 public static bool TryCoerce(string input, out short output)
226 return short.TryParse(input, out output);
229 /// <summary>
230 /// Tries to coerce an string into a Int16.
231 /// </summary>
232 /// <param name="input">the input value</param>
233 /// <param name="output">the output value</param>
234 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
235 public static bool TryCoerce(IPAddress input, out short output)
237 output = default(short);
238 return false;
241 /// <summary>
242 /// Tries to coerce an Boolean into a Int16.
243 /// </summary>
244 /// <param name="input">the input value</param>
245 /// <param name="output">the output value</param>
246 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
247 public static bool TryCoerce(bool input, out short output)
249 output = Convert.ToInt16(input);
250 return true;
253 /// <summary>
254 /// Tries to coerce an Int16 into a UInt32.
255 /// </summary>
256 /// <param name="input">the input value</param>
257 /// <param name="output">the output value</param>
258 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
259 public static bool TryCoerce(short input, out uint output)
261 if (input >= 0)
263 output = Convert.ToUInt32(input);
264 return true;
266 output = default(uint);
267 return false;
270 /// <summary>
271 /// Tries to coerce an UInt16 into a UInt32.
272 /// </summary>
273 /// <param name="input">the input value</param>
274 /// <param name="output">the output value</param>
275 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
276 public static bool TryCoerce(ushort input, out uint output)
278 output = Convert.ToUInt32(input);
279 return true;
282 /// <summary>
283 /// Tries to coerce an Int32 into a UInt32.
284 /// </summary>
285 /// <param name="input">the input value</param>
286 /// <param name="output">the output value</param>
287 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
288 public static bool TryCoerce(int input, out uint output)
290 if (input >= 0)
292 output = Convert.ToUInt32(input);
293 return true;
295 output = default(uint);
296 return false;
299 /// <summary>
300 /// Tries to coerce an Int64 into a UInt32.
301 /// </summary>
302 /// <param name="input">the input value</param>
303 /// <param name="output">the output value</param>
304 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
305 public static bool TryCoerce(long input, out uint output)
307 if (input >= 0 && input <= uint.MaxValue)
309 output = Convert.ToUInt32(input);
310 return true;
312 output = default(uint);
313 return false;
316 /// <summary>
317 /// Tries to coerce an UInt64 into a UInt32.
318 /// </summary>
319 /// <param name="input">the input value</param>
320 /// <param name="output">the output value</param>
321 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
322 public static bool TryCoerce(ulong input, out uint output)
324 if (input <= uint.MaxValue)
326 output = Convert.ToUInt32(input);
327 return true;
329 output = default(uint);
330 return false;
333 /// <summary>
334 /// Tries to coerce an string into a UInt32.
335 /// </summary>
336 /// <param name="input">the input value</param>
337 /// <param name="output">the output value</param>
338 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
339 public static bool TryCoerce(string input, out uint output)
341 return uint.TryParse(input, out output);
344 /// <summary>
345 /// Tries to coerce an IPAddress into a UInt32.
346 /// </summary>
347 /// <param name="input">the input value</param>
348 /// <param name="output">the output value</param>
349 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
350 public static bool TryCoerce(IPAddress input, out uint output)
352 byte[] addr = input.GetAddressBytes();
353 output = BitConverter.ToUInt32(addr, 0);
354 return true;
357 /// <summary>
358 /// Tries to coerce an Boolean into a UInt32.
359 /// </summary>
360 /// <param name="input">the input value</param>
361 /// <param name="output">the output value</param>
362 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
363 public static bool TryCoerce(bool input, out uint output)
365 output = Convert.ToUInt32(input);
366 return true;
369 /// <summary>
370 /// Tries to coerce an Int16 into a Int32.
371 /// </summary>
372 /// <param name="input">the input value</param>
373 /// <param name="output">the output value</param>
374 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
375 public static bool TryCoerce(short input, out int output)
377 output = Convert.ToInt32(input);
378 return true;
381 /// <summary>
382 /// Tries to coerce an UInt16 into a Int32.
383 /// </summary>
384 /// <param name="input">the input value</param>
385 /// <param name="output">the output value</param>
386 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
387 public static bool TryCoerce(ushort input, out int output)
389 output = Convert.ToInt32(input);
390 return true;
393 /// <summary>
394 /// Tries to coerce an UInt32 into a Int32.
395 /// </summary>
396 /// <param name="input">the input value</param>
397 /// <param name="output">the output value</param>
398 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
399 public static bool TryCoerce(uint input, out int output)
401 if (input <= int.MaxValue)
403 output = Convert.ToInt32(input);
404 return true;
406 output = default(int);
407 return false;
410 /// <summary>
411 /// Tries to coerce an Int64 into a Int32.
412 /// </summary>
413 /// <param name="input">the input value</param>
414 /// <param name="output">the output value</param>
415 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
416 public static bool TryCoerce(long input, out int output)
418 if (input >= int.MinValue && input <= int.MaxValue)
420 output = Convert.ToInt32(input);
421 return true;
423 output = default(int);
424 return false;
427 /// <summary>
428 /// Tries to coerce an UInt64 into a Int32.
429 /// </summary>
430 /// <param name="input">the input value</param>
431 /// <param name="output">the output value</param>
432 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
433 public static bool TryCoerce(ulong input, out int output)
435 if (input <= int.MaxValue)
437 output = Convert.ToInt32(input);
438 return true;
440 output = default(int);
441 return false;
444 /// <summary>
445 /// Tries to coerce a string into a Int32.
446 /// </summary>
447 /// <param name="input">the input value</param>
448 /// <param name="output">the output value</param>
449 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
450 public static bool TryCoerce(string input, out int output)
452 return int.TryParse(input, out output);
455 /// <summary>
456 /// Tries to coerce an IPAddress into a Int32.
457 /// </summary>
458 /// <param name="input">the input value</param>
459 /// <param name="output">the output value</param>
460 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
461 public static bool TryCoerce(IPAddress input, out int output)
463 if (input == null)
465 output = default(int);
466 return false;
468 byte[] addr = input.GetAddressBytes();
469 output = BitConverter.ToInt32(addr, 0);
470 return true;
473 /// <summary>
474 /// Tries to coerce an Int16 into a Int32.
475 /// </summary>
476 /// <param name="input">the input value</param>
477 /// <param name="output">the output value</param>
478 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
479 public static bool TryCoerce(bool input, out int output)
481 output = Convert.ToInt32(input);
482 return true;
485 /// <summary>
486 /// Tries to coerce an Int16 into a string.
487 /// </summary>
488 /// <param name="input">the input value</param>
489 /// <param name="output">the output value</param>
490 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
491 public static bool TryCoerce(short input, out string output)
493 output = Convert.ToString(input);
494 return true;
497 /// <summary>
498 /// Tries to coerce an UInt16 into a string.
499 /// </summary>
500 /// <param name="input">the input value</param>
501 /// <param name="output">the output value</param>
502 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
503 public static bool TryCoerce(ushort input, out string output)
505 output = Convert.ToString(input);
506 return true;
509 /// <summary>
510 /// Tries to coerce an UInt32 into a string.
511 /// </summary>
512 /// <param name="input">the input value</param>
513 /// <param name="output">the output value</param>
514 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
515 public static bool TryCoerce(uint input, out string output)
517 output = Convert.ToString(input);
518 return true;
521 /// <summary>
522 /// Tries to coerce an Int32 into a string.
523 /// </summary>
524 /// <param name="input">the input value</param>
525 /// <param name="output">the output value</param>
526 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
527 public static bool TryCoerce(int input, out string output)
529 output = Convert.ToString(input);
530 return true;
533 /// <summary>
534 /// Tries to coerce an Int64 into a string.
535 /// </summary>
536 /// <param name="input">the input value</param>
537 /// <param name="output">the output value</param>
538 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
539 public static bool TryCoerce(long input, out string output)
541 output = Convert.ToString(input);
542 return true;
545 /// <summary>
546 /// Tries to coerce an UInt64 into a string.
547 /// </summary>
548 /// <param name="input">the input value</param>
549 /// <param name="output">the output value</param>
550 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
551 public static bool TryCoerce(ulong input, out string output)
553 output = Convert.ToString(input);
554 return true;
557 /// <summary>
558 /// Tries to coerce an IPAddress into a string.
559 /// </summary>
560 /// <param name="input">the input value</param>
561 /// <param name="output">the output value</param>
562 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
563 public static bool TryCoerce(IPAddress input, out string output)
565 output = Convert.ToString(input);
566 return true;
569 /// <summary>
570 /// Tries to coerce an Boolean into a string.
571 /// </summary>
572 /// <param name="input">the input value</param>
573 /// <param name="output">the output value</param>
574 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
575 public static bool TryCoerce(bool input, out string output)
577 output = Convert.ToString(input);
578 return true;
581 /// <summary>
582 /// Tries to coerce an Int16 into an IPAddress.
583 /// </summary>
584 /// <param name="input">the input value</param>
585 /// <param name="output">the output value</param>
586 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
587 public static bool TryCoerce(short input, out IPAddress output)
589 output = default(IPAddress);
590 return true;
593 /// <summary>
594 /// Tries to coerce an UInt16 into an IPAddress.
595 /// </summary>
596 /// <param name="input">the input value</param>
597 /// <param name="output">the output value</param>
598 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
599 public static bool TryCoerce(ushort input, out IPAddress output)
601 output = default(IPAddress);
602 return true;
605 /// <summary>
606 /// Tries to coerce an UInt32 into an IPAddress.
607 /// </summary>
608 /// <param name="input">the input value</param>
609 /// <param name="output">the output value</param>
610 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
611 public static bool TryCoerce(uint input, out IPAddress output)
613 output = new IPAddress(BitConverter.GetBytes(input));
614 return true;
617 /// <summary>
618 /// Tries to coerce an Int32 into an IPAddress.
619 /// </summary>
620 /// <param name="input">the input value</param>
621 /// <param name="output">the output value</param>
622 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
623 public static bool TryCoerce(int input, out IPAddress output)
625 output = new IPAddress(BitConverter.GetBytes(input));
626 return true;
629 /// <summary>
630 /// Tries to coerce an string into an IPAddress.
631 /// </summary>
632 /// <param name="input">the input value</param>
633 /// <param name="output">the output value</param>
634 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
635 public static bool TryCoerce(string input, out IPAddress output)
637 return IPAddress.TryParse(input, out output);
640 /// <summary>
641 /// Tries to coerce an Int64 into an IPAddress.
642 /// </summary>
643 /// <param name="input">the input value</param>
644 /// <param name="output">the output value</param>
645 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
646 public static bool TryCoerce(long input, out IPAddress output)
648 output = default(IPAddress);
649 return true;
652 /// <summary>
653 /// Tries to coerce an UInt64 into an IPAddress.
654 /// </summary>
655 /// <param name="input">the input value</param>
656 /// <param name="output">the output value</param>
657 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
658 public static bool TryCoerce(ulong input, out IPAddress output)
660 output = default(IPAddress);
661 return true;
664 /// <summary>
665 /// Tries to coerce an Boolean into an IPAddress.
666 /// </summary>
667 /// <param name="input">the input value</param>
668 /// <param name="output">the output value</param>
669 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
670 public static bool TryCoerce(bool input, out IPAddress output)
672 output = default(IPAddress);
673 return true;
676 /// <summary>
677 /// Tries to coerce an Int16 into an UInt64.
678 /// </summary>
679 /// <param name="input">the input value</param>
680 /// <param name="output">the output value</param>
681 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
682 public static bool TryCoerce(short input, out long output)
684 output = Convert.ToInt64(input);
685 return true;
688 /// <summary>
689 /// Tries to coerce an UInt16 into an Int64.
690 /// </summary>
691 /// <param name="input">the input value</param>
692 /// <param name="output">the output value</param>
693 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
694 public static bool TryCoerce(ushort input, out long output)
696 output = Convert.ToInt64(input);
697 return true;
700 /// <summary>
701 /// Tries to coerce an UInt32 into an Int64.
702 /// </summary>
703 /// <param name="input">the input value</param>
704 /// <param name="output">the output value</param>
705 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
706 public static bool TryCoerce(uint input, out long output)
708 output = Convert.ToInt64(input);
709 return true;
712 /// <summary>
713 /// Tries to coerce an Int32 into an Int64.
714 /// </summary>
715 /// <param name="input">the input value</param>
716 /// <param name="output">the output value</param>
717 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
718 public static bool TryCoerce(int input, out long output)
720 output = Convert.ToInt64(input);
721 return true;
724 /// <summary>
725 /// Tries to coerce an UInt64 into an Int64.
726 /// </summary>
727 /// <param name="input">the input value</param>
728 /// <param name="output">the output value</param>
729 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
730 public static bool TryCoerce(ulong input, out long output)
732 if (input <= long.MaxValue)
734 output = Convert.ToInt64(input);
735 return true;
737 output = default(long);
738 return false;
741 /// <summary>
742 /// Tries to coerce an string into an Int64.
743 /// </summary>
744 /// <param name="input">the input value</param>
745 /// <param name="output">the output value</param>
746 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
747 public static bool TryCoerce(string input, out long output)
749 return long.TryParse(input, out output);
752 /// <summary>
753 /// Tries to coerce an IPAddress into an Int64.
754 /// </summary>
755 /// <param name="input">the input value</param>
756 /// <param name="output">the output value</param>
757 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
758 public static bool TryCoerce(IPAddress input, out long output)
760 output = default(long);
761 return false;
764 /// <summary>
765 /// Tries to coerce an Boolean into an Int64.
766 /// </summary>
767 /// <param name="input">the input value</param>
768 /// <param name="output">the output value</param>
769 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
770 public static bool TryCoerce(bool input, out long output)
772 output = Convert.ToInt64(input);
773 return false;
776 /// <summary>
777 /// Tries to coerce an Int16 into an UInt64.
778 /// </summary>
779 /// <param name="input">the input value</param>
780 /// <param name="output">the output value</param>
781 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
782 public static bool TryCoerce(short input, out ulong output)
784 output = Convert.ToUInt64(input);
785 return true;
788 /// <summary>
789 /// Tries to coerce an UInt16 into an UInt64.
790 /// </summary>
791 /// <param name="input">the input value</param>
792 /// <param name="output">the output value</param>
793 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
794 public static bool TryCoerce(ushort input, out ulong output)
796 output = Convert.ToUInt64(input);
797 return true;
800 /// <summary>
801 /// Tries to coerce an UInt32 into an UInt64.
802 /// </summary>
803 /// <param name="input">the input value</param>
804 /// <param name="output">the output value</param>
805 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
806 public static bool TryCoerce(uint input, out ulong output)
808 output = Convert.ToUInt64(input);
809 return true;
812 /// <summary>
813 /// Tries to coerce an Int16 into an UInt64.
814 /// </summary>
815 /// <param name="input">the input value</param>
816 /// <param name="output">the output value</param>
817 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
818 public static bool TryCoerce(long input, out ulong output)
820 if (input > 0)
822 output = Convert.ToUInt64(input);
823 return true;
825 output = default(ulong);
826 return false;
829 /// <summary>
830 /// Tries to coerce an string into an UInt64.
831 /// </summary>
832 /// <param name="input">the input value</param>
833 /// <param name="output">the output value</param>
834 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
835 public static bool TryCoerce(string input, out ulong output)
837 return ulong.TryParse(input, out output);
840 /// <summary>
841 /// Tries to coerce an IPAddress into an UInt64.
842 /// </summary>
843 /// <param name="input">the input value</param>
844 /// <param name="output">the output value</param>
845 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
846 public static bool TryCoerce(IPAddress input, out ulong output)
848 output = default(ulong);
849 return false;
852 /// <summary>
853 /// Tries to coerce an Boolean into an UInt64.
854 /// </summary>
855 /// <param name="input">the input value</param>
856 /// <param name="output">the output value</param>
857 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
858 public static bool TryCoerce(bool input, out ulong output)
860 output = Convert.ToUInt64(input);
861 return false;
864 /// <summary>
865 /// Tries to coerce an Int16 into an Boolean.
866 /// </summary>
867 /// <param name="input">the input value</param>
868 /// <param name="output">the output value</param>
869 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
870 public static bool TryCoerce(short input, out bool output)
872 output = Convert.ToBoolean(input);
873 return true;
876 /// <summary>
877 /// Tries to coerce an UInt16 into an Boolean.
878 /// </summary>
879 /// <param name="input">the input value</param>
880 /// <param name="output">the output value</param>
881 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
882 public static bool TryCoerce(ushort input, out bool output)
884 output = Convert.ToBoolean(input);
885 return true;
888 /// <summary>
889 /// Tries to coerce an UInt16 into an Boolean.
890 /// </summary>
891 /// <param name="input">the input value</param>
892 /// <param name="output">the output value</param>
893 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
894 public static bool TryCoerce(uint input, out bool output)
896 output = Convert.ToBoolean(input);
897 return true;
900 /// <summary>
901 /// Tries to coerce an Int64 into an Boolean.
902 /// </summary>
903 /// <param name="input">the input value</param>
904 /// <param name="output">the output value</param>
905 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
906 public static bool TryCoerce(long input, out bool output)
908 output = Convert.ToBoolean(input);
909 return true;
912 /// <summary>
913 /// Tries to coerce an string into an Boolean.
914 /// </summary>
915 /// <param name="input">the input value</param>
916 /// <param name="output">the output value</param>
917 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
918 public static bool TryCoerce(string input, out bool output)
920 return bool.TryParse(input, out output);
923 /// <summary>
924 /// Tries to coerce an IPAddress into an Boolean.
925 /// </summary>
926 /// <param name="input">the input value</param>
927 /// <param name="output">the output value</param>
928 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
929 public static bool TryCoerce(IPAddress input, out bool output)
931 output = default(bool);
932 return false;
935 /// <summary>
936 /// Tries to coerce an UInt64 into an Boolean.
937 /// </summary>
938 /// <param name="input">the input value</param>
939 /// <param name="output">the output value</param>
940 /// <returns><em>true</em> if the input value is coerced into a value of the output type; otherwise <em>false</em></returns>
941 public static bool TryCoerce(ulong input, out bool output)
943 output = Convert.ToBoolean(input);
944 return true;
947 /// <summary>
948 /// Tries to coerce an input value into a UInt16.
949 /// </summary>
950 /// <typeparam name="V">input value type V</typeparam>
951 /// <param name="tt">TypeToken of the input value</param>
952 /// <param name="input">input value</param>
953 /// <param name="output">reference to a variable to hold the output</param>
954 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
955 internal static bool TryCoerce<V>(TypeToken tt, V input, out ushort output)
957 switch (tt)
959 case TypeToken.UINT16:
960 output = Convert.ToUInt16(input);
961 return true;
962 case TypeToken.INT16:
963 return TryCoerce(Convert.ToInt16(input), out output);
964 case TypeToken.UINT32:
965 return TryCoerce(Convert.ToUInt32(input), out output);
966 case TypeToken.INT32:
967 return TryCoerce(Convert.ToInt32(input), out output);
968 case TypeToken.STRING:
969 return TryCoerce(Convert.ToString(input), out output);
970 case TypeToken.IP_ADDR:
971 return TryCoerce(input as IPAddress, out output);
972 case TypeToken.INT64:
973 return TryCoerce(Convert.ToInt64(input), out output);
974 case TypeToken.UINT64:
975 return TryCoerce(Convert.ToUInt64(input), out output);
976 case TypeToken.BOOLEAN:
977 return TryCoerce(Convert.ToBoolean(input), out output);
979 output = default(ushort);
980 return false;
983 /// <summary>
984 /// Tries to coerce an input value into a Int16.
985 /// </summary>
986 /// <typeparam name="V">input value type V</typeparam>
987 /// <param name="tt">TypeToken of the input value</param>
988 /// <param name="input">input value</param>
989 /// <param name="output">reference to a variable to hold the output</param>
990 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
991 internal static bool TryCoerce<V>(TypeToken tt, V input, out short output)
993 switch (tt)
995 case TypeToken.UINT16:
996 return TryCoerce(Convert.ToUInt16(input), out output);
997 case TypeToken.INT16:
998 output = Convert.ToInt16(input);
999 return true;
1000 case TypeToken.UINT32:
1001 return TryCoerce(Convert.ToUInt32(input), out output);
1002 case TypeToken.INT32:
1003 return TryCoerce(Convert.ToInt32(input), out output);
1004 case TypeToken.STRING:
1005 return TryCoerce(Convert.ToString(input), out output);
1006 case TypeToken.IP_ADDR:
1007 return TryCoerce(input as IPAddress, out output);
1008 case TypeToken.INT64:
1009 return TryCoerce(Convert.ToInt64(input), out output);
1010 case TypeToken.UINT64:
1011 return TryCoerce(Convert.ToUInt64(input), out output);
1012 case TypeToken.BOOLEAN:
1013 return TryCoerce(Convert.ToBoolean(input), out output);
1015 output = default(short);
1016 return false;
1019 /// <summary>
1020 /// Tries to coerce an input value into a Int32.
1021 /// </summary>
1022 /// <typeparam name="V">input value type V</typeparam>
1023 /// <param name="tt">TypeToken of the input value</param>
1024 /// <param name="input">input value</param>
1025 /// <param name="output">reference to a variable to hold the output</param>
1026 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1027 internal static bool TryCoerce<V>(TypeToken tt, V input, out uint output)
1029 switch (tt)
1031 case TypeToken.UINT16:
1032 output = Convert.ToUInt16(input);
1033 return true;
1034 case TypeToken.INT16:
1035 return TryCoerce(Convert.ToInt16(input), out output);
1036 case TypeToken.UINT32:
1037 output = Convert.ToUInt32(input);
1038 return true;
1039 case TypeToken.INT32:
1040 return TryCoerce(Convert.ToInt32(input), out output);
1041 case TypeToken.STRING:
1042 return TryCoerce(Convert.ToString(input), out output);
1043 case TypeToken.IP_ADDR:
1044 return TryCoerce(input as IPAddress, out output);
1045 case TypeToken.INT64:
1046 return TryCoerce(Convert.ToInt64(input), out output);
1047 case TypeToken.UINT64:
1048 return TryCoerce(Convert.ToUInt64(input), out output);
1049 case TypeToken.BOOLEAN:
1050 return TryCoerce(Convert.ToBoolean(input), out output);
1052 output = default(ushort);
1053 return false;
1056 /// <summary>
1057 /// Tries to coerce an input value into a Int32.
1058 /// </summary>
1059 /// <typeparam name="V">input value type V</typeparam>
1060 /// <param name="tt">TypeToken of the input value</param>
1061 /// <param name="input">input value</param>
1062 /// <param name="output">reference to a variable to hold the output</param>
1063 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1064 internal static bool TryCoerce<V>(TypeToken tt, V input, out int output)
1066 switch (tt)
1068 case TypeToken.UINT16:
1069 return TryCoerce(Convert.ToUInt16(input), out output);
1070 case TypeToken.INT16:
1071 return TryCoerce(Convert.ToInt16(input), out output);
1072 case TypeToken.UINT32:
1073 return TryCoerce(Convert.ToUInt32(input), out output);
1074 case TypeToken.INT32:
1075 output = Convert.ToInt32(input);
1076 return true;
1077 case TypeToken.STRING:
1078 return TryCoerce(Convert.ToString(input), out output);
1079 case TypeToken.IP_ADDR:
1080 return TryCoerce(input as IPAddress, out output);
1081 case TypeToken.INT64:
1082 return TryCoerce(Convert.ToInt64(input), out output);
1083 case TypeToken.UINT64:
1084 return TryCoerce(Convert.ToUInt64(input), out output);
1085 case TypeToken.BOOLEAN:
1086 return TryCoerce(Convert.ToBoolean(input), out output);
1088 output = default(ushort);
1089 return false;
1092 /// <summary>
1093 /// Tries to coerce an input value into a String.
1094 /// </summary>
1095 /// <typeparam name="V">input value type V</typeparam>
1096 /// <param name="tt">TypeToken of the input value</param>
1097 /// <param name="input">input value</param>
1098 /// <param name="output">reference to a variable to hold the output</param>
1099 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1100 internal static bool TryCoerce<V>(TypeToken tt, V input, out string output)
1102 switch (tt)
1104 case TypeToken.UINT16:
1105 return TryCoerce(Convert.ToUInt16(input), out output);
1106 case TypeToken.INT16:
1107 return TryCoerce(Convert.ToInt16(input), out output);
1108 case TypeToken.UINT32:
1109 return TryCoerce(Convert.ToUInt32(input), out output);
1110 case TypeToken.INT32:
1111 return TryCoerce(Convert.ToInt32(input), out output);
1112 case TypeToken.STRING:
1113 output = Convert.ToString(input);
1114 return true;
1115 case TypeToken.IP_ADDR:
1116 return TryCoerce(input as IPAddress, out output);
1117 case TypeToken.INT64:
1118 return TryCoerce(Convert.ToInt64(input), out output);
1119 case TypeToken.UINT64:
1120 return TryCoerce(Convert.ToUInt64(input), out output);
1121 case TypeToken.BOOLEAN:
1122 return TryCoerce(Convert.ToBoolean(input), out output);
1124 output = default(string);
1125 return false;
1128 /// <summary>
1129 /// Tries to coerce an input value into a IPAddress.
1130 /// </summary>
1131 /// <typeparam name="V">input value type V</typeparam>
1132 /// <param name="tt">TypeToken of the input value</param>
1133 /// <param name="input">input value</param>
1134 /// <param name="output">reference to a variable to hold the output</param>
1135 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1136 internal static bool TryCoerce<V>(TypeToken tt, V input, out IPAddress output)
1138 switch (tt)
1140 case TypeToken.UINT16:
1141 return TryCoerce(Convert.ToUInt16(input), out output);
1142 case TypeToken.INT16:
1143 return TryCoerce(Convert.ToInt16(input), out output);
1144 case TypeToken.UINT32:
1145 return TryCoerce(Convert.ToUInt32(input), out output);
1146 case TypeToken.INT32:
1147 return TryCoerce(Convert.ToInt32(input), out output);
1148 case TypeToken.STRING:
1149 return TryCoerce(Convert.ToString(input), out output);
1150 case TypeToken.IP_ADDR:
1151 output = input as IPAddress;
1152 return true;
1153 case TypeToken.INT64:
1154 return TryCoerce(Convert.ToInt64(input), out output);
1155 case TypeToken.UINT64:
1156 return TryCoerce(Convert.ToUInt64(input), out output);
1157 case TypeToken.BOOLEAN:
1158 return TryCoerce(Convert.ToBoolean(input), out output);
1160 output = default(IPAddress);
1161 return false;
1164 /// <summary>
1165 /// Tries to coerce an input value into a Int64.
1166 /// </summary>
1167 /// <typeparam name="V">input value type V</typeparam>
1168 /// <param name="tt">TypeToken of the input value</param>
1169 /// <param name="input">input value</param>
1170 /// <param name="output">reference to a variable to hold the output</param>
1171 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1172 internal static bool TryCoerce<V>(TypeToken tt, V input, out long output)
1174 switch (tt)
1176 case TypeToken.UINT16:
1177 return TryCoerce(Convert.ToUInt16(input), out output);
1178 case TypeToken.INT16:
1179 return TryCoerce(Convert.ToInt16(input), out output);
1180 case TypeToken.UINT32:
1181 return TryCoerce(Convert.ToUInt32(input), out output);
1182 case TypeToken.INT32:
1183 return TryCoerce(Convert.ToInt32(input), out output);
1184 case TypeToken.STRING:
1185 return TryCoerce(Convert.ToString(input), out output);
1186 case TypeToken.IP_ADDR:
1187 return TryCoerce(input as IPAddress, out output);
1188 case TypeToken.INT64:
1189 output = Convert.ToUInt16(input);
1190 return true;
1191 case TypeToken.UINT64:
1192 return TryCoerce(Convert.ToUInt64(input), out output);
1193 case TypeToken.BOOLEAN:
1194 return TryCoerce(Convert.ToBoolean(input), out output);
1196 output = default(ushort);
1197 return false;
1200 /// <summary>
1201 /// Tries to coerce an input value into a UInt64.
1202 /// </summary>
1203 /// <typeparam name="V">input value type V</typeparam>
1204 /// <param name="tt">TypeToken of the input value</param>
1205 /// <param name="input">input value</param>
1206 /// <param name="output">reference to a variable to hold the output</param>
1207 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1208 internal static bool TryCoerce<V>(TypeToken tt, V input, out ulong output)
1210 switch (tt)
1212 case TypeToken.UINT16:
1213 return TryCoerce(Convert.ToUInt16(input), out output);
1214 case TypeToken.INT16:
1215 return TryCoerce(Convert.ToInt16(input), out output);
1216 case TypeToken.UINT32:
1217 return TryCoerce(Convert.ToUInt32(input), out output);
1218 case TypeToken.INT32:
1219 return TryCoerce(Convert.ToInt32(input), out output);
1220 case TypeToken.STRING:
1221 return TryCoerce(Convert.ToString(input), out output);
1222 case TypeToken.IP_ADDR:
1223 return TryCoerce(input as IPAddress, out output);
1224 case TypeToken.INT64:
1225 return TryCoerce(Convert.ToInt64(input), out output);
1226 case TypeToken.UINT64:
1227 output = Convert.ToUInt64(input);
1228 return true;
1229 case TypeToken.BOOLEAN:
1230 return TryCoerce(Convert.ToBoolean(input), out output);
1232 output = default(ulong);
1233 return false;
1236 /// <summary>
1237 /// Tries to coerce an input value into a Boolean.
1238 /// </summary>
1239 /// <typeparam name="V">input value type V</typeparam>
1240 /// <param name="tt">TypeToken of the input value</param>
1241 /// <param name="input">input value</param>
1242 /// <param name="output">reference to a variable to hold the output</param>
1243 /// <returns><em>true</em> if the input is successfully coerced; otherwise <em>false</em></returns>
1244 internal static bool TryCoerce<V>(TypeToken tt, V input, out bool output)
1246 switch (tt)
1248 case TypeToken.UINT16:
1249 return TryCoerce(Convert.ToUInt16(input), out output);
1250 case TypeToken.INT16:
1251 return TryCoerce(Convert.ToInt16(input), out output);
1252 case TypeToken.UINT32:
1253 return TryCoerce(Convert.ToUInt32(input), out output);
1254 case TypeToken.INT32:
1255 return TryCoerce(Convert.ToInt32(input), out output);
1256 case TypeToken.STRING:
1257 return TryCoerce(Convert.ToString(input), out output);
1258 case TypeToken.IP_ADDR:
1259 return TryCoerce(input as IPAddress, out output);
1260 case TypeToken.INT64:
1261 return TryCoerce(Convert.ToInt64(input), out output);
1262 case TypeToken.UINT64:
1263 return TryCoerce(Convert.ToUInt64(input), out output);
1264 case TypeToken.BOOLEAN:
1265 output = Convert.ToBoolean(input);
1266 return true;
1268 output = default(bool);
1269 return false;
1272 #endregion Methods