You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

using System.Collections.Generic;
namespace VOL.Core.Utils
{
public class CommonUtil
{
// 实体对象转Dictionary
public static Dictionary<string, object> ConvertToDictionary<T>(T entity)
{
var dictionary = new Dictionary<string, object>();
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
var key = property.Name;
var value = property.GetValue(entity);
dictionary.Add(key, value);
}
return dictionary;
}
// Dictionary转实体对象
public static T ConvertToObject<T>(Dictionary<string, object> dictionary) where T : new()
{
var obj = new T();
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
if (dictionary.TryGetValue(property.Name, out var value))
{
property.SetValue(obj, value);
}
}
return obj;
}
}
}