.netcore 使用反射处理实体类中时间保存Mongodb自动减8小时
public class ChangeDateTimeKind
{
public static async Task ToUtc<T>(T model)
{
Type NewType = typeof(T);
foreach (PropertyInfo item in NewType.GetRuntimeProperties())
{
string Name = item.PropertyType.Name;
if (Name.ToLower()=="datetime")
{
await Task.Run(() =>
{
DateTime getDateTime = Convert.ToDateTime(item.GetValue(model, null));
object v = Convert.ChangeType(DateTime.SpecifyKind(getDateTime, DateTimeKind.Utc), item.PropertyType);
item.SetValue(model, v, null);
});
}
}
}
}

浙公网安备 33010602011771号