/// <summary> /// Searches for LimitAttribute attributes on a property. /// </summary> /// <param name="prop">The property with annotations.</param> /// <returns>The Limit attribute if present, or null if none is.</returns> internal static LimitAttribute FindFrom(PropertyInfo prop) { LimitAttribute fieldLimits = null; foreach (var attr in prop.GetCustomAttributes(false)) { if ((fieldLimits = CreateFrom(attr)) != null) { break; } } return(fieldLimits); }
/// <summary> /// Creates a LimitAttribute using an object from another mod. /// </summary> /// <param name="attr">The attribute from the other mod.</param> /// <returns>A LimitAttribute object with the values from that object, where /// possible to retrieve; or null if none could be obtained.</returns> private static LimitAttribute CreateFrom(object attr) { LimitAttribute la = null; if (attr.GetType().Name == typeof(LimitAttribute).Name) { // Has limit type var trAttr = Traverse.Create(attr); double min = 0.0, max = 0.0; try { min = trAttr.GetProperty <double>(nameof(Minimum)); max = trAttr.GetProperty <double>(nameof(Maximum)); } catch (Exception e) { PUtil.LogExcWarn(e); } if (min != 0.0 || max != 0.0) { la = new LimitAttribute(min, max); } } return(la); }