public async Task<IActionResult> AddUserInfo([FromBody]vUpdateModel infoModel)
{
ApiReturn<long> apiReturn = new ApiReturn<long>();
try
{
SysUser user = new SysUser();
var props = user.GetType().GetProperties();
for (int i = 0; i < infoModel.PropNames.Count; i++)
{
var property = user.GetType().GetProperty(infoModel.PropNames[i]);
if (property == null)
throw new Exception("“" + infoModel.PropNames[i] + "”不存在");
var value = infoModel.Values[i];
var sType = value.GetType();
Type dType = property.PropertyType;
var dConverter = TypeDescriptor.GetConverter(dType);
if (dConverter != null && dConverter.CanConvertFrom(value.GetType()))
{
object desValue;
try
{
desValue = dConverter.ConvertFromString(null, CultureInfo.InvariantCulture, value);
}
catch(Exception ex)
{
throw new Exception("“" + infoModel.PropNames[i] + "”对应的值“" + infoModel.Values[i] + "”数据类型错误");
}
property.SetValue(user, desValue);
}
}
}
catch (Exception ex)
{
apiReturn.Success = false;
apiReturn.StatusCode = (int)ApiEnum.Error;
apiReturn.Message = ex.Message;
}
return Ok(apiReturn);
}
public class vUpdateModel
{
public vUpdateModel()
{
PropNames = new List<string>();
Values = new List<string>();
}
public long Id { get; set; }
public List<string> PropNames { get; set; }
public List<string> Values { get; set; }
}