/// <summary> Gets the attribute of type T from mem. </summary> /// <typeparam name="T"></typeparam> /// <param name="mem"></param> /// <returns></returns> static public T GetAttribute <T>(this IReflectionData mem) { if (typeof(T) == typeof(DisplayAttribute) && mem is TypeData td) { return((T)((object)td.Display)); } if (mem.Attributes is object[] array) { // performance optimization: faster iterations if we know its an array. foreach (var thing in array) { if (thing is T x) { return(x); } } } else { foreach (var thing in mem.Attributes) { if (thing is T x) { return(x); } } } return(default);
/// <summary> /// Returns true if a reflection ifno has an attribute of type T. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mem"></param> /// <returns></returns> static public bool HasAttribute <T>(this IReflectionData mem) where T : class { return(mem.GetAttribute <T>() != null); }