/Reflection.cs |
@@ -8,7 +8,6 @@ |
using System.Collections.Generic; |
using System.Linq; |
using System.Reflection; |
using wasSharp; |
|
namespace wasSharp |
{ |
@@ -25,7 +24,7 @@ |
{ |
return (T) value.GetType() |
.GetRuntimeField(value.ToString()) |
.GetCustomAttributes(typeof (T), false) |
.GetCustomAttributes(typeof(T), false) |
.SingleOrDefault(); |
} |
|
@@ -54,9 +53,9 @@ |
public static IEnumerable<string> GetEnumNames<T>() |
{ |
return |
typeof (T).GetRuntimeFields().ToArray() |
typeof(T).GetRuntimeFields().ToArray() |
.AsParallel() |
.Select(o => o.GetCustomAttribute(typeof (NameAttribute), false)) |
.Select(o => o.GetCustomAttribute(typeof(NameAttribute), false)) |
.Select(o => (o as NameAttribute)?.Name) |
.Where(o => !string.IsNullOrEmpty(o)) |
.Select(o => o); |
@@ -71,7 +70,7 @@ |
/// <returns>the values of the enumeration</returns> |
public static IEnumerable<T> GetEnumValues<T>() |
{ |
return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value); |
return Enum.GetValues(typeof(T)).Cast<object>().Select(value => (T) value); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
@@ -86,7 +85,7 @@ |
{ |
var attribute = value.GetType() |
.GetRuntimeField(value.ToString()) |
.GetCustomAttributes(typeof (NameAttribute), false) |
.GetCustomAttributes(typeof(NameAttribute), false) |
.SingleOrDefault() as NameAttribute; |
return attribute?.Name; |
} |
@@ -103,7 +102,7 @@ |
{ |
var attribute = value.GetType() |
.GetRuntimeField(value.ToString()) |
.GetCustomAttributes(typeof (DescriptionAttribute), false) |
.GetCustomAttributes(typeof(DescriptionAttribute), false) |
.SingleOrDefault() as DescriptionAttribute; |
return attribute?.Description; |
} |
@@ -119,11 +118,12 @@ |
/// <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().ToArray() |
var field = typeof(T).GetRuntimeFields().ToArray() |
.AsParallel().SelectMany(f => f.GetCustomAttributes( |
typeof (NameAttribute), false), ( |
f, a) => new {Field = f, Att = a}).SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) |
.Name, name, StringComparison.Ordinal)); |
typeof(NameAttribute), false), ( |
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); |
} |
|
@@ -139,8 +139,8 @@ |
/// <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().ToArray() |
.AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), |
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; |
} |
@@ -232,7 +232,7 @@ |
public static IEnumerable<Type> GetBaseTypes(this Type type) |
{ |
var baseType = type.GetTypeInfo().BaseType; |
if(baseType == null) |
if (baseType == null) |
yield break; |
yield return baseType; |
foreach (var t in GetBaseTypes(baseType)) |