📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
1.6 KiB

// -----------------------------------------------------------------------
// <copyright file="EnumExtensions.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// -----------------------------------------------------------------------
namespace ImageProcessor.Helpers.Extensions
{
#region Using
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
#endregion
/// <summary>
/// Encapsulates a series of time saving extension methods to <see cref="T:System.Enum">Enum</see>s.
/// </summary>
public static class EnumExtensions
{
#region Methods
/// <summary>
/// Extends the <see cref="T:System.Enum">Enum</see> type to return the description attribute for the given type.
/// Useful for when the type to match in the data source contains spaces.
/// </summary>
/// <param name="expression">The given <see cref="T:System.Enum">Enum</see> that this method extends.</param>
/// <returns>A string containing the Enum's description attribute.</returns>
public static string ToDescription(this Enum expression)
{
Contract.Requires(expression != null);
DescriptionAttribute[] descriptionAttribute =
(DescriptionAttribute[])
expression.GetType().GetField(expression.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : expression.ToString();
}
#endregion
}
}