Files
Web_HotelServices_Prod/SERVER/LIB/DicObject.cs
2025-11-26 11:18:26 +08:00

48 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SERVER.LIB
{
public class DicObject<T> where T : new()
{
public static List<T> GetList(params IDictionary<string, string>[] dic)
{
List<T> res = new List<T>();
Type tp = typeof(T);
var info = tp.GetProperties();
foreach (var item in dic)
{
var tump = new T();
foreach (var item1 in item)
{
var v = info.FirstOrDefault(x => x.Name.ToLower() == item1.Key.ToLower());
if (v != null && v.CanRead && v.CanWrite && item1.Value != null && item1.Value.ToLower() != "null")
{
if (!v.PropertyType.IsGenericType)
{
var val = v.PropertyType.Name == 1.GetType().Name? item1.Value.Split(".")[0]: item1.Value;
v.SetValue(tump, string.IsNullOrEmpty(val) ? null : Convert.ChangeType(val, v.PropertyType),null);
}
else
{
//泛型Nullable<>
Type genericTypeDefinition = v.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
v.SetValue(tump, string.IsNullOrEmpty(item1.Value) ? null : Convert.ChangeType(item1.Value, Nullable.GetUnderlyingType(v.PropertyType)), null);
}
}
}
}
res.Add(tump);
}
return res;
}
}
}