|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RuleOfReady.Core.Utility
{
/// <summary>
/// Extension methods for enums
/// </summary>
/// <remarks>
/// Multiple posters at stackoverflow.com helped with these methods
/// </remarks>
public static class EnumExtensions
{
/// <summary>
/// Generic version of GetValues method (why isn't this in the .Net Framework already?)
/// </summary>
/// <typeparam name="TEnum">Type of an enum</typeparam>
/// <param name="someEnum"></param>
/// <returns></returns>
/// <remarks>
/// There is no proper type check for an Enum. This is as close as possible in C#
/// </remarks>
public static IEnumerable<TEnum> GetValues<TEnum>(this TEnum someEnum)
where TEnum : struct, IConvertible, IComparable, IFormattable
{
if (!typeof(TEnum).IsEnum)
throw new ArgumentException(string.Format("{0} must be an enumerated type",typeof(TEnum).FullName));
return Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm a software developer that works with a small team on websites in .Net. Software development is a career of constant learning, and here I'm learning by sharing.