using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection;
///
/// 反射相关工具集
///
public partial class Utils
{
///
/// C#对象根据属性名获取值
///
///
///
///
public static object GetPropertyValue(object csObj, string property)
{
PropertyInfo pinfo = csObj.GetType().GetProperty(property);
if (pinfo == null) return null;
return pinfo.GetValue(csObj, null);
}
///
/// C#对象根据属性名赋值
///
///
///
///
public static void SetPropertyValue(object csObj, string property, object value)
{
if (value == null) return;
PropertyInfo pinfo = csObj.GetType().GetProperty(property);
if (pinfo != null)
{
if (pinfo.PropertyType == typeof(string))
{
pinfo.SetValue(csObj, value.ToString(), null);
}
else if (pinfo.PropertyType == typeof(float))
{
pinfo.SetValue(csObj, float.Parse(value.ToString()), null);
}
else if (pinfo.PropertyType == typeof(double))
{
pinfo.SetValue(csObj, double.Parse(value.ToString()), null);
}
else if (pinfo.PropertyType == typeof(int))
{
pinfo.SetValue(csObj, int.Parse(value.ToString()), null);
}
else if (pinfo.PropertyType == typeof(long))
{
pinfo.SetValue(csObj, long.Parse(value.ToString()), null);
}
else
{
csObj.GetType().GetProperty(property).SetValue(csObj, value, null);
}
}
}
///
/// C#获取对象全部属性名
///
///
///
public static List GetProperties(object csObj)
{
List properties = new List();
foreach (PropertyInfo pinfo in csObj.GetType().GetProperties())
{
properties.Add(pinfo.Name);
}
return properties;
}
///
/// C#通过反射机制初始化特定类型对象
///
///
///
///
public static T LoadClass(JObject jObj) where T : new()
{
return LoadObject(jObj, new T());
}
///
/// C#通过反射机制初始化特定类型对象
///
///
///
///
public static T LoadClass(JToken token) where T : new()
{
return LoadObject(token, new T());
}
///
/// C#通过属性表反射原理从JObject加载实例对象
///
///
///
///
///
public static T LoadObject(JObject jObj, T csObj)
{
foreach (KeyValuePair pair in jObj)
{
SetPropertyValue(csObj, pair.Key, pair.Value.ToString());
}
return csObj;
}
///
/// C#通过属性表反射原理从JToken加载实例对象
///
///
///
///
///
public static T LoadObject(JToken token, T csObj)
{
JObject jObj = JObject.Parse(token.ToString());
return LoadObject(jObj, csObj);
}
///
/// C#通过属性表反射原理从JArry加载数组
///
///
///
///
public static List LoadArray(JArray array) where T : new()
{
List ret = new List();
foreach (JToken token in array)
{
ret.Add(LoadClass(token));
}
return ret;
}
///
/// C#通过属性表反射原理从JArry加载数组
///
///
///
///
public static List LoadArray(JToken token) where T : new()
{
return LoadArray(JArray.Parse(token.ToString()));
}
/*
///
/// C#原生对象克隆
///
///
///
///
public static T CloneObject(T obj) where T : new()
{
T desObj = new T();
foreach (PropertyInfo pinfo in desObj.GetType().GetProperties())
{
SetPropertyValue(desObj, pinfo.Name, GetPropertyValue(obj, pinfo.Name));
}
return desObj;
}
* */
///
/// C#原生对象属性表映射加载
///
/// 源对象
/// 目标对象
public static void LoadObject(object surObj, object desObj)
{
foreach (PropertyInfo pinfo in desObj.GetType().GetProperties())
{
SetPropertyValue(desObj, pinfo.Name, GetPropertyValue(surObj, pinfo.Name));
}
}
///
/// List随机其中一个元素
///
///
///
///
public static T ArrayRandom(List array)
{
int count = array.Count;
int ran = UnityEngine.Random.Range(0, count);
return array[ran];
}
}