在LitJson裏面有個類是JsonMapper 映射
有些方法
public static T ToObject<T>(JsonReader reader);
public static T ToObject<T>(TextReader reader);
public static T ToObject<T>(string json);
//使用以上的方法要注意 T類中 字段或者屬性名字 必須是公開的 且和json中的key值保持一致; 如果數據很多可以用List<T>
或者 T[ ] 來取得。
就是通過Reflection 反射 取得 字段 或者屬性,進行匹配的
下面是 泛型類通過反射 去得字段屬性的方法 可以傳個string 進行處理。
public static void PrintAttr<T>() where T :new()
{
T a = default(T);
a = new T();
Type t = a.GetType();
//都是公共的
FieldInfo[] fieldInfos = t.GetFields();//字段
PropertyInfo[] propertyInfos = t.GetProperties();//屬性
MethodInfo[] methodInfos = t.GetMethods();//方法
WriteLine("屬性個數:" + propertyInfos.Count());
foreach (PropertyInfo pInfo in propertyInfos)
{
WriteLine("屬性:" + pInfo.ToString());
}
WriteLine("字段總數:" + fieldInfos.Count());
foreach (var item in fieldInfos)
{
WriteLine("字段" + item.ToString()); //獲取字段
}
WriteLine($"方法總數:{ methodInfos.Count()}");
foreach (MethodInfo item in methodInfos)
{
WriteLine($"方法:{item.ToString()}");
}
WriteLine(t.Assembly);
}