7 /// Utility class for coercing attribute values.
9 public static class Coercion
14 /// Tries to coerce an Int16 into a UInt16.
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
)
23 output
= Convert
.ToUInt16(input
);
26 output
= default(ushort);
31 /// Tries to coerce an Int32 into a UInt16.
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
);
43 output
= default(ushort);
48 /// Tries to coerce an UInt32 into a UInt16.
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
);
60 output
= default(ushort);
65 /// Tries to coerce an Int64 into a UInt16.
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
);
77 output
= default(ushort);
82 /// Tries to coerce an UInt64 into a UInt16.
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
);
94 output
= default(ushort);
99 /// Tries to coerce an string into a UInt16.
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
);
110 /// Tries to coerce an string into a UInt16.
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);
122 /// Tries to coerce an bool into a UInt16.
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
);
134 /// Tries to coerce an UInt16 into a Int16.
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
);
146 output
= default(short);
151 /// Tries to coerce an Int32 into a Int16.
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
);
163 output
= default(short);
168 /// Tries to coerce an UInt32 into a Int16.
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
);
180 output
= default(short);
185 /// Tries to coerce an Int64 into a Int16.
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
);
197 output
= default(short);
202 /// Tries to coerce an UInt64 into a Int16.
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
);
214 output
= default(short);
219 /// Tries to coerce an string into a Int16.
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
);
230 /// Tries to coerce an string into a Int16.
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);
242 /// Tries to coerce an Boolean into a Int16.
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
);
254 /// Tries to coerce an Int16 into a UInt32.
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
)
263 output
= Convert
.ToUInt32(input
);
266 output
= default(uint);
271 /// Tries to coerce an UInt16 into a UInt32.
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
);
283 /// Tries to coerce an Int32 into a UInt32.
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
)
292 output
= Convert
.ToUInt32(input
);
295 output
= default(uint);
300 /// Tries to coerce an Int64 into a UInt32.
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
);
312 output
= default(uint);
317 /// Tries to coerce an UInt64 into a UInt32.
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
);
329 output
= default(uint);
334 /// Tries to coerce an string into a UInt32.
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
);
345 /// Tries to coerce an IPAddress into a UInt32.
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);
358 /// Tries to coerce an Boolean into a UInt32.
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
);
370 /// Tries to coerce an Int16 into a Int32.
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
);
382 /// Tries to coerce an UInt16 into a Int32.
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
);
394 /// Tries to coerce an UInt32 into a Int32.
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
);
406 output
= default(int);
411 /// Tries to coerce an Int64 into a Int32.
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
);
423 output
= default(int);
428 /// Tries to coerce an UInt64 into a Int32.
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
);
440 output
= default(int);
445 /// Tries to coerce a string into a Int32.
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
);
456 /// Tries to coerce an IPAddress into a Int32.
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
)
465 output
= default(int);
468 byte[] addr
= input
.GetAddressBytes();
469 output
= BitConverter
.ToInt32(addr
, 0);
474 /// Tries to coerce an Int16 into a Int32.
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
);
486 /// Tries to coerce an Int16 into a string.
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
);
498 /// Tries to coerce an UInt16 into a string.
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
);
510 /// Tries to coerce an UInt32 into a string.
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
);
522 /// Tries to coerce an Int32 into a string.
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
);
534 /// Tries to coerce an Int64 into a string.
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
);
546 /// Tries to coerce an UInt64 into a string.
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
);
558 /// Tries to coerce an IPAddress into a string.
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
);
570 /// Tries to coerce an Boolean into a string.
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
);
582 /// Tries to coerce an Int16 into an IPAddress.
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
);
594 /// Tries to coerce an UInt16 into an IPAddress.
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
);
606 /// Tries to coerce an UInt32 into an IPAddress.
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
));
618 /// Tries to coerce an Int32 into an IPAddress.
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
));
630 /// Tries to coerce an string into an IPAddress.
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
);
641 /// Tries to coerce an Int64 into an IPAddress.
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
);
653 /// Tries to coerce an UInt64 into an IPAddress.
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
);
665 /// Tries to coerce an Boolean into an IPAddress.
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
);
677 /// Tries to coerce an Int16 into an UInt64.
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
);
689 /// Tries to coerce an UInt16 into an Int64.
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
);
701 /// Tries to coerce an UInt32 into an Int64.
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
);
713 /// Tries to coerce an Int32 into an Int64.
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
);
725 /// Tries to coerce an UInt64 into an Int64.
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
);
737 output
= default(long);
742 /// Tries to coerce an string into an Int64.
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
);
753 /// Tries to coerce an IPAddress into an Int64.
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);
765 /// Tries to coerce an Boolean into an Int64.
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
);
777 /// Tries to coerce an Int16 into an UInt64.
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
);
789 /// Tries to coerce an UInt16 into an UInt64.
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
);
801 /// Tries to coerce an UInt32 into an UInt64.
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
);
813 /// Tries to coerce an Int16 into an UInt64.
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
)
822 output
= Convert
.ToUInt64(input
);
825 output
= default(ulong);
830 /// Tries to coerce an string into an UInt64.
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
);
841 /// Tries to coerce an IPAddress into an UInt64.
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);
853 /// Tries to coerce an Boolean into an UInt64.
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
);
865 /// Tries to coerce an Int16 into an Boolean.
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
);
877 /// Tries to coerce an UInt16 into an Boolean.
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
);
889 /// Tries to coerce an UInt16 into an Boolean.
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
);
901 /// Tries to coerce an Int64 into an Boolean.
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
);
913 /// Tries to coerce an string into an Boolean.
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
);
924 /// Tries to coerce an IPAddress into an Boolean.
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);
936 /// Tries to coerce an UInt64 into an Boolean.
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
);
948 /// Tries to coerce an input value into a UInt16.
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
)
959 case TypeToken
.UINT16
:
960 output
= Convert
.ToUInt16(input
);
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);
984 /// Tries to coerce an input value into a Int16.
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
)
995 case TypeToken
.UINT16
:
996 return TryCoerce(Convert
.ToUInt16(input
), out output
);
997 case TypeToken
.INT16
:
998 output
= Convert
.ToInt16(input
);
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);
1020 /// Tries to coerce an input value into a Int32.
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
)
1031 case TypeToken
.UINT16
:
1032 output
= Convert
.ToUInt16(input
);
1034 case TypeToken
.INT16
:
1035 return TryCoerce(Convert
.ToInt16(input
), out output
);
1036 case TypeToken
.UINT32
:
1037 output
= Convert
.ToUInt32(input
);
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);
1057 /// Tries to coerce an input value into a Int32.
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
)
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
);
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);
1093 /// Tries to coerce an input value into a String.
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
)
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
);
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);
1129 /// Tries to coerce an input value into a IPAddress.
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
)
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
;
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
);
1165 /// Tries to coerce an input value into a Int64.
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
)
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
);
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);
1201 /// Tries to coerce an input value into a UInt64.
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
)
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
);
1229 case TypeToken
.BOOLEAN
:
1230 return TryCoerce(Convert
.ToBoolean(input
), out output
);
1232 output
= default(ulong);
1237 /// Tries to coerce an input value into a Boolean.
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
)
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
);
1268 output
= default(bool);