-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnumExtensions.cs
More file actions
98 lines (88 loc) · 3.4 KB
/
EnumExtensions.cs
File metadata and controls
98 lines (88 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RexStudios.CsharpExtensions
{
public static class EnumExtensions
{
/// <summary>
/// Checks if the target enumeration includes the specified flag.
/// </summary>
/// <remarks>
/// From http://www.codeproject.com/KB/cs/fun-with-cs-extensions.aspx?msg=2838918#xx2838918xx
/// </remarks>
/// <param name="target">The target.</param>
/// <param name="flags">The flags.</param>
/// <returns><b>true</b> if match, otherwise <b>false</b>.</returns>
public static bool Includes<TEnum>(this TEnum target, TEnum flags) where TEnum : IComparable, IConvertible, IFormattable
{
if (target.GetType() != flags.GetType())
{
throw new ArgumentException("Enum type mismatch", "flags");
}
long a = Convert.ToInt64(target);
long b = Convert.ToInt64(flags);
return (a & b) == b;
}
/// <summary>
/// Checks if the target enumeration includes any of the specified flags
/// </summary>
/// <remarks>
/// From http://www.codeproject.com/KB/cs/fun-with-cs-extensions.aspx?msg=2838918#xx2838918xx
/// </remarks>
/// <param name="target">The target.</param>
/// <param name="flags">The flags.</param>
/// <returns><b>true</b> if match, otherwise <b>false</b>.</returns>
public static bool IncludesAny<TEnum>(this TEnum target, TEnum flags) where TEnum : IComparable, IConvertible, IFormattable
{
if (target.GetType() != flags.GetType())
{
throw new ArgumentException("Enum type mismatch", "flags");
}
long a = Convert.ToInt64(target);
long b = Convert.ToInt64(flags);
return (a & b) != 0L;
}
public static string ToDescriptionString(this Enum val)
{
var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
/// <summary>
/// https://www.extensionmethod.net/csharp/enum/enum-t-parse-and-enum-t-tryparse
/// Parses the specified string value into the Enum type passed. Also contains a bool to determine whether or not the case should be ignored.
/// </summary>
/// <typeparam name="T"></typeparam>
public static class Enum<T>
{
public static T Parse(string value)
{
return Enum<T>.Parse(value, true);
}
public static T Parse(string value, bool ignoreCase)
{
return (T)Enum.Parse(typeof(T), value, ignoreCase);
}
public static bool TryParse(string value, out T returnedValue)
{
return Enum<T>.TryParse(value, true, out returnedValue);
}
public static bool TryParse(string value, bool ignoreCase, out T returnedValue)
{
try
{
returnedValue = (T)Enum.Parse(typeof(T), value, ignoreCase);
return true;
}
catch
{
returnedValue = default(T);
return false;
}
}
}
}