Files
CMS-MTConn/MTC_Adapter/SCMA/EnumExtensions.cs
T
Samuele E. Locatelli 4469241cef Typo FIX
2018-08-21 16:50:59 +02:00

95 lines
2.2 KiB
C#

using System;
namespace SCMA
{
public static class EnumerationExtensions
{
/// <summary>
/// checks if the value contains the provided type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Has<T>(this System.Enum type, T value)
{
try
{
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
catch
{
return false;
}
}
/// <summary>
/// checks if the value is only the provided type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Is<T>(this System.Enum type, T value)
{
try
{
return (int)(object)type == (int)(object)value;
}
catch
{
return false;
}
}
/// <summary>
/// appends a value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T Add<T>(this System.Enum type, T value)
{
try
{
return (T)(object)(((int)(object)type | (int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not append value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
/// <summary>
/// completely removes the value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T Remove<T>(this System.Enum type, T value)
{
try
{
return (T)(object)(((int)(object)type & ~(int)(object)value));
}
catch (Exception ex)
{
throw new ArgumentException(
string.Format(
"Could not remove value from enumerated type '{0}'.",
typeof(T).Name
), ex);
}
}
}
}