初始化
This commit is contained in:
47
SERVER/LIB/DicObject.cs
Normal file
47
SERVER/LIB/DicObject.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user