|
@@ -5,7 +5,9 @@
|
|
* ==============================================================================*/
|
|
* ==============================================================================*/
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
|
+using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
|
+using System.Reflection;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
@@ -45,6 +47,78 @@ namespace SAGA.DotNetUtils.Extend
|
|
}
|
|
}
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// JObject 转化为实体类
|
|
|
|
+ /// 通过Description标记
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="jObject"></param>
|
|
|
|
+ /// <param name="type"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static Object ConvertToInstance(this JObject jObject, Type type)
|
|
|
|
+ {
|
|
|
|
+ var obj = type.Assembly.CreateInstance(type.FullName);
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ var properties = obj.GetType().GetProperties();
|
|
|
|
+
|
|
|
|
+ foreach (var propertyInfo in properties)
|
|
|
|
+ {
|
|
|
|
+ var descriptionAttribute = propertyInfo.GetCustomAttribute<DescriptionAttribute>();
|
|
|
|
+ if (descriptionAttribute == null) continue;
|
|
|
|
+ var description = descriptionAttribute.Description;
|
|
|
|
+ var jToken = jObject[description];
|
|
|
|
+ if (jToken == null) continue;
|
|
|
|
+ var ptype = propertyInfo.PropertyType;
|
|
|
|
+
|
|
|
|
+ if (propertyInfo.PropertyType == typeof(string))
|
|
|
|
+ {//String类型
|
|
|
|
+ var str = jToken.Value<string>();
|
|
|
|
+ propertyInfo.SetValue(obj, str);
|
|
|
|
+ }
|
|
|
|
+ else if (propertyInfo.PropertyType.IsGenericType)
|
|
|
|
+ {//集合Content
|
|
|
|
+ Type genericType = ptype.GetGenericArguments().First();
|
|
|
|
+ //创建集合
|
|
|
|
+ object genericList = CreateGeneric(typeof(List<>), genericType);
|
|
|
|
+ if (genericList != null)
|
|
|
|
+ {
|
|
|
|
+ var addMethods = genericList.GetType().GetMethods().FirstOrDefault(t => t.Name == "Add");
|
|
|
|
+ if (addMethods != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (JObject jItem in (JArray)jToken)
|
|
|
|
+ {//集合赋值
|
|
|
|
+ var subObj = ConvertToInstance(jItem, genericType);
|
|
|
|
+ addMethods.Invoke(genericList, new object[] { subObj });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ propertyInfo.SetValue(obj, genericList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return obj;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 初始化集合
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="generic"></param>
|
|
|
|
+ /// <param name="innerType"></param>
|
|
|
|
+ /// <param name="args"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static object CreateGeneric(Type generic, Type innerType, params object[] args)
|
|
|
|
+ {
|
|
|
|
+ Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
|
|
|
|
+ return Activator.CreateInstance(specificType, args);
|
|
|
|
+ }
|
|
public static JObject Merge(this JObject jObject, JObject jObject2)
|
|
public static JObject Merge(this JObject jObject, JObject jObject2)
|
|
{
|
|
{
|
|
JObject result = new JObject();
|
|
JObject result = new JObject();
|