///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace wasSharp
{
public static class Reflection
{
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Retrieves an attribute of type T from an enumeration.
///
/// an attribute of type T
public static T GetAttributeFromEnumValue(Enum value) where T : Attribute
{
return (T)value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof(T), false)
.SingleOrDefault();
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Returns all the attributes of type T of an enumeration.
///
/// the attribute to retrieve
/// a list of attributes
public static IEnumerable GetEnumAttributes(Enum e) where T : Attribute
{
return e.GetType().GetRuntimeFields().ToArray()
.AsParallel()
.Select(o => GetAttributeFromEnumValue((Enum)o.GetValue(Activator.CreateInstance())));
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Returns all the field names of an enumeration.
///
/// the field names
public static IEnumerable GetEnumNames()
{
return
typeof(T).GetRuntimeFields().ToArray()
.AsParallel()
.Select(o => o.GetCustomAttribute(typeof(NameAttribute), false))
.Select(o => (o as NameAttribute)?.Name)
.Where(o => !string.IsNullOrEmpty(o))
.Select(o => o);
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Returns all the values of an enumeration.
///
/// the values of the enumeration
public static IEnumerable GetEnumValues()
{
return Enum.GetValues(typeof(T)).Cast