/// <summary> /// Adds a log to the local model /// </summary> /// <param name="log">The log to add</param> /// <param name="SupressTypeMismatch">If set to true, this method will not throw an exception if the log result does not match the workout type</param> public void AddLog(WL_Log log, bool SupressTypeMismatch = false) { log.Workout = this.GetWorkout(log.Workout); if (!IsWellTyped(log) && !SupressTypeMismatch) { throw new WL_Exception($"Workout type {log.Workout.GetType().Name} is not compatible with result type {log.Result.GetType().Name}"); } Logs.Add(log); }
/// <summary> /// Returns true if the result of the log is a valid type for the workout of the log /// </summary> /// <param name="log"></param> /// <returns></returns> private bool IsWellTyped(WL_Log log) { //Get the result type of the log Type resultType = log.Result.GetType(); //Check to make sure the result type has a compatibilty attribute if (!resultType.IsDefined(typeof(WL_ResultCompatabilityAttribute))) { return(false); } //Extract the attribute WL_ResultCompatabilityAttribute compatibiiltyAtt = resultType.GetCustomAttribute <WL_ResultCompatabilityAttribute>(); //check the workout type of the log against all the possible compatible types of the result foreach (Type compatibleType in compatibiiltyAtt.WorkoutTypes) { if (compatibleType.IsAssignableFrom(log.Workout.GetType())) { return(true); } } //None of the compatible types matched, so return false return(false); }