示例#1
0
        public IActionResult UpdateProperty(Guid id, string property, string value)
        {
            if (string.IsNullOrWhiteSpace(property))
            {
                return(BadRequest($"Missing: 'property' value"));
            }
            if (value == null)
            {
                return(BadRequest($"Missing: 'value' value"));
            }

            var effect = effectController.GetEffect(id);

            if (effect == null)
            {
                return(BadRequest($"Effect with the given Id was not found. Id: {id}"));
            }

            if (string.IsNullOrWhiteSpace(property))
            {
                return(BadRequest($"Missing: 'property' value"));
            }

            PropertyInfo propertyInfo = effect.GetType().GetProperty(property);

            if (propertyInfo == null)
            {
                return(BadRequest($"Property not found. Property: {property}"));
            }

            object newValue = null;

            try
            {
                newValue = Convert.ChangeType(value, propertyInfo.PropertyType);
            }
            catch
            {
                return(BadRequest($"Invalid value type, expected: {propertyInfo.PropertyType.ToString()}"));
            }

            try
            {
                propertyInfo.SetValue(effect, newValue, null);
            }
            catch (Exception e)
            {
                return(BadRequest($"Failed to set value. " + e.ToString()));
            }

            return(Ok());
        }