fx-sharp  0.1
A collection of functional extensions for C#.
 All Classes Namespaces Files Functions Variables Enumerator
IntegralTypeExtensions.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace FxSharp.Extensions
4 {
5  public static class IntegralTypeExtensions
6  {
7  /// <summary>
8  /// Parse an int to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
9  /// </summary>
10  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
11  /// <param name="value">The int representation of the enumeration or underlying value.</param>
12  /// <returns>The wrapped value.</returns>
13  public static Maybe<T> ParseEnum<T>(this int value)
14  where T : struct
15  {
16  return Enum.IsDefined(typeof (T), value)
17  ? Maybe.Just((T) (ValueType) value)
18  : Maybe.Nothing<T>();
19  }
20 
21  /// <summary>
22  /// Parse an uint to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
23  /// </summary>
24  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
25  /// <param name="value">The unit representation of the enumeration or underlying value.</param>
26  /// <returns>The wrapped value.</returns>
27  public static Maybe<T> ParseEnum<T>(this uint value)
28  where T : struct
29  {
30  return Enum.IsDefined(typeof (T), value)
31  ? Maybe.Just((T) (ValueType) value)
32  : Maybe.Nothing<T>();
33  }
34 
35  /// <summary>
36  /// Parse a long to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
37  /// </summary>
38  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
39  /// <param name="value">The lopng representation of the enumeration or underlying value.</param>
40  /// <returns>The wrapped value.</returns>
41  public static Maybe<T> ParseEnum<T>(this long value)
42  where T : struct
43  {
44  return Enum.IsDefined(typeof (T), value)
45  ? Maybe.Just((T) (ValueType) value)
46  : Maybe.Nothing<T>();
47  }
48 
49  /// <summary>
50  /// Parse a ulong to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
51  /// </summary>
52  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
53  /// <param name="value">The ulong representation of the enumeration or underlying value.</param>
54  /// <returns>The wrapped value.</returns>
55  public static Maybe<T> ParseEnum<T>(this ulong value)
56  where T : struct
57  {
58  return Enum.IsDefined(typeof (T), value)
59  ? Maybe.Just((T) (ValueType) value)
60  : Maybe.Nothing<T>();
61  }
62 
63  /// <summary>
64  /// Parse a short to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
65  /// </summary>
66  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
67  /// <param name="value">The short representation of the enumeration or underlying value.</param>
68  /// <returns>The wrapped value.</returns>
69  public static Maybe<T> ParseEnum<T>(this short value)
70  where T : struct
71  {
72  return Enum.IsDefined(typeof (T), value)
73  ? Maybe.Just((T) (ValueType) value)
74  : Maybe.Nothing<T>();
75  }
76 
77  /// <summary>
78  /// Parse a ushort to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
79  /// </summary>
80  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
81  /// <param name="value">The ushort representation of the enumeration or underlying value.</param>
82  /// <returns>The wrapped value.</returns>
83  public static Maybe<T> ParseEnum<T>(this ushort value)
84  where T : struct
85  {
86  return Enum.IsDefined(typeof (T), value)
87  ? Maybe.Just((T) (ValueType) value)
88  : Maybe.Nothing<T>();
89  }
90 
91  /// <summary>
92  /// Parse a byte to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
93  /// </summary>
94  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
95  /// <param name="value">The byte representation of the enumeration or underlying value.</param>
96  /// <returns>The wrapped value.</returns>
97  public static Maybe<T> ParseEnum<T>(this byte value)
98  where T : struct
99  {
100  return Enum.IsDefined(typeof (T), value)
101  ? Maybe.Just((T) (ValueType) value)
102  : Maybe.Nothing<T>();
103  }
104 
105  /// <summary>
106  /// Parse a sbyte to an enum value. Unlike Enum.TryParse it returns a result wrapped in Maybe.
107  /// </summary>
108  /// <typeparam name="T">The enumeration type to which to convert the value.</typeparam>
109  /// <param name="value">The sbyte representation of the enumeration or underlying value.</param>
110  /// <returns>The wrapped value.</returns>
111  public static Maybe<T> ParseEnum<T>(this sbyte value)
112  where T : struct
113  {
114  return Enum.IsDefined(typeof (T), value)
115  ? Maybe.Just((T) (ValueType) value)
116  : Maybe.Nothing<T>();
117  }
118  }
119 }
Property</c ></item > *< item >< c > value