// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// Enum Helpers
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Utilities
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using HandBrake.Interop.Attributes;
///
/// Enum Helpers
///
///
/// The Type Parameter
///
public class EnumHelper
{
///
/// Get the description of an Enum
///
///
/// The value.
///
///
/// The Description string
///
public static string GetDescription(T value)
{
if (value == null)
{
return string.Empty;
}
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
if (fieldInfo != null)
{
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
return string.Empty;
}
///
/// Get the Display Value of the Enum Model
///
/// An Enum with Display Attributes
/// A string name
public static string GetDisplay(T value)
{
if (value == null)
{
return string.Empty;
}
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
if (fieldInfo != null)
{
DisplayName[] attributes = (DisplayName[])fieldInfo.GetCustomAttributes(typeof(DisplayName), false);
return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
}
return string.Empty;
}
///
/// Get the Enumeration for a given Enum Description
///
/// The String description
/// The Enum Value
public static T GetValue(string description)
{
return GetValue(description, false);
}
///
/// Get the Enumeration for a given Enum Description
///
/// The String description
/// Turn of sensitivity to cases.
/// The Enum Value
public static T GetValue(string description, bool insensitiveCase)
{
foreach (T val in Enum.GetValues(typeof(T)))
{
string currDescription = GetDescription(val);
string currDisplay = GetDisplay(val);
string shortName = GetShortName(val);
if (currDescription == description || currDisplay == description || shortName == description)
{
return val;
}
if (insensitiveCase && (currDescription.ToLower() == description.ToLower() || currDisplay.ToLower() == description.ToLower() || shortName.ToLower() == description.ToLower()))
{
return val;
}
}
Debug.WriteLine("EnumHelper.GetValue: The Description for the enum was not recognized: " + description);
return default(T);
}
///
/// The get short name.
///
///
/// The value.
///
///
/// The .
///
public static string GetShortName(T value)
{
if (value == null)
{
return string.Empty;
}
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
if (fieldInfo != null)
{
ShortName[] attributes = (ShortName[])fieldInfo.GetCustomAttributes(typeof(ShortName), false);
return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
}
return string.Empty;
}
public static TH GetAttribute(TK value) where TH : Attribute
{
if (value == null)
{
return null;
}
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
if (fieldInfo != null)
{
TH[] attributes = (TH[])fieldInfo.GetCustomAttributes(typeof(TH), false);
return (attributes.Length > 0) ? attributes[0] : null;
}
return null;
}
///
/// Return a list of all the enum values.
///
///
/// An Enum Oject List
///
public static IEnumerable GetEnumList()
{
return Enum.GetValues(typeof(T)).Cast().ToList();
}
///
/// Get a list of string names for each enum value.
///
///
/// The enum type.
///
///
/// A collection of strings that represent all the enum values
///
public static IEnumerable GetEnumDisplayValues(Type enumType)
{
var strings = new Collection();
foreach (T e in Enum.GetValues(enumType))
{
strings.Add(GetDisplay(e));
}
return strings;
}
///
/// Get a list of string names for each enum value passed in.
///
///
/// The items.
///
///
/// A collection of strings that represent all the enum values
///
public static IEnumerable GetEnumDisplayValuesSubset(IEnumerable items)
{
return items.Select(GetDisplay).ToList();
}
}
}
|