corrade-vassal

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 15  →  ?path2? @ 16
/wasSharp/Reflection.cs
@@ -11,7 +11,7 @@
 
namespace wasSharp
{
public class Reflection
public static class Reflection
{
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
@@ -38,7 +38,7 @@
/// <returns>a list of attributes</returns>
public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute
{
return e.GetType().GetRuntimeFields()
return e.GetType().GetRuntimeFields().ToArray()
.AsParallel()
.Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>())));
}
@@ -53,7 +53,7 @@
public static IEnumerable<string> GetEnumNames<T>()
{
return
typeof (T).GetRuntimeFields()
typeof (T).GetRuntimeFields().ToArray()
.AsParallel()
.Select(o => o.GetCustomAttribute(typeof (NameAttribute), false))
.Select(o => (o as NameAttribute)?.Name)
@@ -83,7 +83,7 @@
/// <returns>the description or the empty string</returns>
public static string GetNameFromEnumValue(Enum value)
{
NameAttribute attribute = value.GetType()
var attribute = value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof (NameAttribute), false)
.SingleOrDefault() as NameAttribute;
@@ -100,7 +100,7 @@
/// <returns>the description or the empty string</returns>
public static string GetDescriptionFromEnumValue(Enum value)
{
DescriptionAttribute attribute = value.GetType()
var attribute = value.GetType()
.GetRuntimeField(value.ToString())
.GetCustomAttributes(typeof (DescriptionAttribute), false)
.SingleOrDefault() as DescriptionAttribute;
@@ -118,7 +118,7 @@
/// <returns>the value or the default of T if case no name attribute found</returns>
public static T GetEnumValueFromName<T>(string name)
{
var field = typeof (T).GetRuntimeFields()
var field = typeof (T).GetRuntimeFields().ToArray()
.AsParallel().SelectMany(f => f.GetCustomAttributes(
typeof (NameAttribute), false), (
f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att)
@@ -130,7 +130,7 @@
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Get the description of structure member.
/// Get the name of a structure member.
/// </summary>
/// <typeparam name="T">the type of the structure to search</typeparam>
/// <param name="structure">the structure to search</param>
@@ -138,13 +138,32 @@
/// <returns>the description or the empty string</returns>
public static string GetStructureMemberName<T>(T structure, object item) where T : struct
{
var field = typeof (T).GetRuntimeFields()
var field = typeof (T).GetRuntimeFields().ToArray()
.AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false),
(f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item));
return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Get enumeration value from its description.
/// </summary>
/// <typeparam name="T">the enumeration type</typeparam>
/// <param name="description">the description of a member</param>
/// <returns>the value or the default of T if case no description found</returns>
public static T GetEnumValueFromDescription<T>(string description)
{
var field = typeof(T).GetRuntimeFields().ToArray()
.AsParallel().SelectMany(f => f.GetCustomAttributes(
typeof(DescriptionAttribute), false), (
f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((DescriptionAttribute)a.Att)
.Description.Equals(description));
return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
}
 
/// <summary>
/// A generic name attribute.
/// </summary>
public class NameAttribute : Attribute