/Reflection.cs |
@@ -8,10 +8,11 @@ |
using System.Collections.Generic; |
using System.Linq; |
using System.Reflection; |
using wasSharp; |
|
namespace wasSharp |
{ |
public class Reflection |
public static class Reflection |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -38,7 +39,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 +54,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) |
@@ -77,13 +78,13 @@ |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Get the description from an enumeration value. |
/// Get the name from an enumeration value. |
/// </summary> |
/// <param name="value">an enumeration value</param> |
/// <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 +101,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,11 +119,11 @@ |
/// <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) |
.Name.Equals(name)); |
f, a) => new {Field = f, Att = a}).SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) |
.Name, name, StringComparison.Ordinal)); |
return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
} |
|
@@ -130,7 +131,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 +139,109 @@ |
/// <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) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Get field or property from a class by supplying a path. |
/// </summary> |
/// <typeparam name="T">the type of the object</typeparam> |
/// <param name="o">the object</param> |
/// <param name="path">the fully qualified path to the field of property</param> |
/// <returns> |
/// the last object in the fully qualified path or null in case the field or property could not be found |
/// </returns> |
public static object GetFP<T>(this T o, string path) |
{ |
if (string.IsNullOrEmpty(path)) return null; |
if (o == null) return null; |
|
var memberType = o.GetType(); |
var components = path.Split('.'); |
|
var f = memberType.GetRuntimeField(components[0]); |
var p = memberType.GetRuntimeProperty(components[0]); |
|
if (f != null) |
return components.Length > 1 |
? GetFP(f.GetValue(o), |
components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
: memberType.GetRuntimeField(path).GetValue(o); |
|
if (p != null) |
return components.Length > 1 |
? GetFP(p.GetValue(o), |
components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
: memberType.GetRuntimeProperty(path).GetValue(o); |
|
return null; |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Get field or property info from a class by supplying a path. |
/// </summary> |
/// <typeparam name="T">the type of the object</typeparam> |
/// <param name="o">the object</param> |
/// <param name="path">the fully qualified path to the field of property</param> |
/// <returns> |
/// the field or property info of the last object in the path or null if the object cannot be found |
/// </returns> |
public static object GetFPInfo<T>(this T o, string path) |
{ |
if (string.IsNullOrEmpty(path)) return null; |
if (o == null) return null; |
|
var memberType = o.GetType(); |
var components = path.Split('.'); |
|
var f = memberType.GetRuntimeField(components[0]); |
var p = memberType.GetRuntimeProperty(components[0]); |
|
if (f != null) |
return components.Length > 1 |
? GetFPInfo(f.GetValue(o), |
components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
: memberType.GetRuntimeField(path); |
|
if (p != null) |
return components.Length > 1 |
? GetFPInfo(p.GetValue(o), |
components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
: memberType.GetRuntimeProperty(path); |
|
return null; |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Enumerate all the base types recursively starting from a type. |
/// </summary> |
/// <param name="type">the type</param> |
/// <returns>an enumeration of all base types</returns> |
public static IEnumerable<Type> GetBaseTypes(this Type type) |
{ |
var baseType = type.GetTypeInfo().BaseType; |
if(baseType == null) |
yield break; |
yield return baseType; |
foreach (var t in GetBaseTypes(baseType)) |
{ |
yield return t; |
} |
} |
|
/// <summary> |
/// A generic name attribute. |
/// </summary> |
public class NameAttribute : Attribute |