2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
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/>.
26 /// Utility class for coercing attribute values.
28 public static class Coercion
33 /// Tries to coerce an Int16 into a UInt16.
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
)
42 output
= Convert
.ToUInt16(input
);
45 output
= default(ushort);
50 /// Tries to coerce an Int32 into a UInt16.
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
);
62 output
= default(ushort);
67 /// Tries to coerce an UInt32 into a UInt16.
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
);
79 output
= default(ushort);
84 /// Tries to coerce an Int64 into a UInt16.
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
);
96 output
= default(ushort);
101 /// Tries to coerce an UInt64 into a UInt16.
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
);
113 output
= default(ushort);
118 /// Tries to coerce an string into a UInt16.
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
);
129 /// Tries to coerce an string into a UInt16.
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);
141 /// Tries to coerce an bool into a UInt16.
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
);
153 /// Tries to coerce an UInt16 into a Int16.
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
);
165 output
= default(short);
170 /// Tries to coerce an Int32 into a Int16.
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
);
182 output
= default(short);
187 /// Tries to coerce an UInt32 into a Int16.
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
);
199 output
= default(short);
204 /// Tries to coerce an Int64 into a Int16.
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
);
216 output
= default(short);
221 /// Tries to coerce an UInt64 into a Int16.
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
);
233 output
= default(short);
238 /// Tries to coerce an string into a Int16.
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
);
249 /// Tries to coerce an string into a Int16.
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);
261 /// Tries to coerce an Boolean into a Int16.
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
);
273 /// Tries to coerce an Int16 into a UInt32.
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
)
282 output
= Convert
.ToUInt32(input
);
285 output
= default(uint);
290 /// Tries to coerce an UInt16 into a UInt32.
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
);
302 /// Tries to coerce an Int32 into a UInt32.
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
)
311 output
= Convert
.ToUInt32(input
);
314 output
= default(uint);
319 /// Tries to coerce an Int64 into a UInt32.
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
);
331 output
= default(uint);
336 /// Tries to coerce an UInt64 into a UInt32.
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
);
348 output
= default(uint);
353 /// Tries to coerce an string into a UInt32.
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
);
364 /// Tries to coerce an IPAddress into a UInt32.
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);
377 /// Tries to coerce an Boolean into a UInt32.
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
);
389 /// Tries to coerce an Int16 into a Int32.
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
);
401 /// Tries to coerce an UInt16 into a Int32.
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
);
413 /// Tries to coerce an UInt32 into a Int32.
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
);
425 output
= default(int);
430 /// Tries to coerce an Int64 into a Int32.
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
);
442 output
= default(int);
447 /// Tries to coerce an UInt64 into a Int32.
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
);
459 output
= default(int);
464 /// Tries to coerce a string into a Int32.
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
);
475 /// Tries to coerce an IPAddress into a Int32.
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
)
484 output
= default(int);
487 byte[] addr
= input
.GetAddressBytes();
488 output
= BitConverter
.ToInt32(addr
, 0);
493 /// Tries to coerce an Int16 into a Int32.
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
);
505 /// Tries to coerce an Int16 into a string.
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
);
517 /// Tries to coerce an UInt16 into a string.
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
);
529 /// Tries to coerce an UInt32 into a string.
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
);
541 /// Tries to coerce an Int32 into a string.
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
);
553 /// Tries to coerce an Int64 into a string.
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
);
565 /// Tries to coerce an UInt64 into a string.
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
);
577 /// Tries to coerce an IPAddress into a string.
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
);
589 /// Tries to coerce an Boolean into a string.
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
);
601 /// Tries to coerce an Int16 into an IPAddress.
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
);
613 /// Tries to coerce an UInt16 into an IPAddress.
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
);
625 /// Tries to coerce an UInt32 into an IPAddress.
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
));
637 /// Tries to coerce an Int32 into an IPAddress.
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
));
649 /// Tries to coerce an string into an IPAddress.
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
);
660 /// Tries to coerce an Int64 into an IPAddress.
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
);
672 /// Tries to coerce an UInt64 into an IPAddress.
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
);
684 /// Tries to coerce an Boolean into an IPAddress.
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
);
696 /// Tries to coerce an Int16 into an UInt64.
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
);
708 /// Tries to coerce an UInt16 into an Int64.
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
);
720 /// Tries to coerce an UInt32 into an Int64.
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
);
732 /// Tries to coerce an Int32 into an Int64.
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
);
744 /// Tries to coerce an UInt64 into an Int64.
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
);
756 output
= default(long);
761 /// Tries to coerce an string into an Int64.
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
);
772 /// Tries to coerce an IPAddress into an Int64.
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);
784 /// Tries to coerce an Boolean into an Int64.
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
);
796 /// Tries to coerce an Int16 into an UInt64.
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
);
808 /// Tries to coerce an UInt16 into an UInt64.
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
);
820 /// Tries to coerce an UInt32 into an UInt64.
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
);
832 /// Tries to coerce an Int16 into an UInt64.
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
)
841 output
= Convert
.ToUInt64(input
);
844 output
= default(ulong);
849 /// Tries to coerce an string into an UInt64.
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
);
860 /// Tries to coerce an IPAddress into an UInt64.
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);
872 /// Tries to coerce an Boolean into an UInt64.
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
);
884 /// Tries to coerce an Int16 into an Boolean.
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
);
896 /// Tries to coerce an UInt16 into an Boolean.
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
);
908 /// Tries to coerce an UInt16 into an Boolean.
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
);
920 /// Tries to coerce an Int64 into an Boolean.
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
);
932 /// Tries to coerce an string into an Boolean.
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
);
943 /// Tries to coerce an IPAddress into an Boolean.
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);
955 /// Tries to coerce an UInt64 into an Boolean.
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
);
967 /// Tries to coerce an input value into a UInt16.
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
)
978 case TypeToken
.UINT16
:
979 output
= Convert
.ToUInt16(input
);
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);
1003 /// Tries to coerce an input value into a Int16.
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
)
1014 case TypeToken
.UINT16
:
1015 return TryCoerce(Convert
.ToUInt16(input
), out output
);
1016 case TypeToken
.INT16
:
1017 output
= Convert
.ToInt16(input
);
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);
1039 /// Tries to coerce an input value into a Int32.
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
)
1050 case TypeToken
.UINT16
:
1051 output
= Convert
.ToUInt16(input
);
1053 case TypeToken
.INT16
:
1054 return TryCoerce(Convert
.ToInt16(input
), out output
);
1055 case TypeToken
.UINT32
:
1056 output
= Convert
.ToUInt32(input
);
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);
1076 /// Tries to coerce an input value into a Int32.
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
)
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
);
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);
1112 /// Tries to coerce an input value into a String.
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
)
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
);
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);
1148 /// Tries to coerce an input value into a IPAddress.
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
)
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
;
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
);
1184 /// Tries to coerce an input value into a Int64.
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
)
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
);
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);
1220 /// Tries to coerce an input value into a UInt64.
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
)
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
);
1248 case TypeToken
.BOOLEAN
:
1249 return TryCoerce(Convert
.ToBoolean(input
), out output
);
1251 output
= default(ulong);
1256 /// Tries to coerce an input value into a Boolean.
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
)
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
);
1287 output
= default(bool);