예제 #1
0
        private Task InvokeStatusUpdateMethod(RawShipStatus raw, IShipStatus status, string propertyName)
        {
            try
            {
                var rawValue             = raw.GetType().GetProperty(propertyName).GetValue(raw);
                var statusUpdateProperty = _status.GetType().GetProperty(propertyName).GetValue(_status);
                var updateMethod         = statusUpdateProperty.GetType()
                                           .GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance);
                var needsUpdateMethod = statusUpdateProperty.GetType()
                                        .GetMethod("NeedsUpdate", BindingFlags.NonPublic | BindingFlags.Instance);

                var needsUpdate = needsUpdateMethod.Invoke(statusUpdateProperty, new[] { rawValue });
                if ((bool)needsUpdate)
                {
                    _log.LogTrace("Invoking OnChange event for {name} status", propertyName);
                    updateMethod.Invoke(statusUpdateProperty, new[] { this, rawValue });
                }
            }
            catch (Exception ex)
            {
                _log.LogWarning(ex, "Could not update status property {name}", propertyName);
            }

            return(Task.CompletedTask);
        }
예제 #2
0
        private async Task InvokeStatusMethods(string json)
        {
            try
            {
                RawShipStatus raw = JsonConvert.DeserializeObject <RawShipStatus>(json);

                foreach (string propertyName in _status.GetType().GetProperties().Select(x => x.Name))
                {
                    await InvokeStatusUpdateMethod(raw, _status, propertyName);
                }

                _status.TriggerOnChange();
            }
            catch (Exception ex)
            {
                _log.LogWarning(ex, "Could not update status");
            }
        }