123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEngine;
- using XRTool.Util;
- namespace PublicTools.XMLDataBase
- {
-
-
-
-
-
- public static class ClazzFactory
- {
-
-
-
-
-
- public static string GetPriKey<T>(T t, string priKey)
- {
- if (t == null)
- {
- UnityLog.LogError(typeof(T) + " is null" + priKey);
- }
- if (string.IsNullOrEmpty(priKey))
- {
- UnityLog.LogError(typeof(T) + "prikey is null:" + priKey);
- }
- var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
- if (fields == null || fields.Length < 1)
- {
- priKey=JsonConvert.SerializeObject(t.GetType().GetProperty(priKey).GetValue(t, null)).Replace("\"", "");
- }
- else
- {
- priKey = JsonConvert.SerializeObject(t.GetType().GetField(priKey).GetValue(t)).Replace("\"", "");
- }
- return priKey;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static Dictionary<string, string> ClazzAnalyze<T>(T t)
- {
- Dictionary<string, string> field = new Dictionary<string, string>();
- if (t == null)
- {
- t = default(T);
- }
- if (t != null)
- {
-
- var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
- bool isHas = false;
-
- if (fields != null && fields.Length > 0)
- {
- for (int i = 0; i < fields.Length; i++)
- {
- if (fields[i].FieldType.ToString().Contains("Dictionary"))
- {
- continue;
- }
- string value = "";
- string key = fields[i].Name;
- try
- {
- value = JsonConvert.SerializeObject(t.GetType().GetField(key).GetValue(t)).Replace("\"", "");
- }
- catch (Exception ex)
- {
-
- Debug.LogError(ex.ToString() + typeof(T).Name + "--" + fields[i].Name + " is error value");
- }
- field.Add(key, value);
- isHas = true;
- }
- }
- if (!isHas)
- {
-
- var property = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- if (property != null && property.Length > 0)
- {
- for (int i = 0; i < property.Length; i++)
- {
- if (property[i].PropertyType.ToString().Contains("Dictionary"))
- {
- continue;
- }
- string value = "";
- string key = property[i].Name;
- try
- {
- value = JsonConvert.SerializeObject(t.GetType().GetProperty(key).GetValue(t, null)).Replace("\"", "");
- }
- catch (Exception ex)
- {
-
- Debug.LogError(ex.ToString() + typeof(T).Name + "--" + property[i].Name + " is error value");
- }
-
- field.Add(key, value);
- }
- }
- }
- }
- else
- {
- UnityLog.LogError(typeof(T).Name + " is null");
- }
- return field;
- }
-
-
-
-
-
-
-
-
-
-
- public static T ClazzPack<T>(Dictionary<string, string> field)
- {
- T t = (T)(Assembly.GetExecutingAssembly().CreateInstance(typeof(T).Namespace + "." + typeof(T).Name));
- var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
- bool isHas = false;
-
- if (fields != null && fields.Length > 0)
- {
- for (int i = 0; i < fields.Length; i++)
- {
- if (field.ContainsKey(fields[i].Name))
- {
- object tempArr;
- try
- {
- if (field.ContainsKey(fields[i].Name))
- {
- string value = field[fields[i].Name];
-
- if (value.StartsWith("{") && value.EndsWith("}"))
- {
- string realValue = "{";
- string nullFlag = value.Replace("{", "").Replace("}", "");
- string[] arr = nullFlag.Split(',');
- for (int j = 0; j < arr.Length; j++)
- {
- string[] tmpaar = arr[j].Split(':');
- string shuxing = tmpaar[0];
- shuxing = "\"" + shuxing + "\"";
- realValue += shuxing + ":" + tmpaar[1];
- if (j < arr.Length - 1)
- {
- realValue += ",";
- }
- }
- value = realValue + "}";
- }
- else if (value.StartsWith("[") && value.EndsWith("]"))
- {
- string realValue = "[";
- string nullFlag = value.Replace("[", "").Replace("]", "");
- string[] arr = nullFlag.Split(',');
- for (int j = 0; j < arr.Length; j++)
- {
- realValue += "\"" + arr[j] + "\"";
- if (j < arr.Length - 1)
- {
- realValue += ",";
- }
- }
- value = realValue + "]";
- }
- else
- {
- if (!value.StartsWith("\"") && !value.EndsWith("\""))
- {
- value = "\"" + value + "\"";
- }
- }
-
- if (value == "null")
- {
- tempArr = null;
- }
- else
- {
- tempArr = JsonConvert.DeserializeObject(value, fields[i].FieldType);
- }
- fields[i].SetValue(t, tempArr);
- isHas = true;
- }
- else
- {
- UnityLog.LogError(fields[i].Name + " is null!");
- }
- }
- catch (Exception ex)
- {
-
- UnityLog.LogError(ex.ToString() + typeof(T).Name + "--" + fields[i].Name + field[fields[i].Name] + " is error value");
- }
- }
- }
- }
- if (!isHas)
- {
-
- PropertyInfo[] property = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
- for (int i = 0; i < property.Length; i++)
- {
- if (field.ContainsKey(property[i].Name))
- {
- object tempArr;
- try
- {
- if (field.ContainsKey(property[i].Name))
- {
- string value = field[property[i].Name];
-
- if (value.StartsWith("{") && value.EndsWith("}"))
- {
- string realValue = "{";
- string nullFlag = value.Replace("{", "").Replace("}", "");
- string[] arr = nullFlag.Split(',');
- for (int j = 0; j < arr.Length; j++)
- {
- string[] tmpaar = arr[j].Split(':');
- string shuxing = tmpaar[0];
- shuxing = "\"" + shuxing + "\"";
- realValue += shuxing + ":" + tmpaar[1];
- if (j < arr.Length - 1)
- {
- realValue += ",";
- }
- }
- value = realValue + "}";
- }
- else
- {
- if (!value.StartsWith("\"") && !value.EndsWith("\""))
- {
- value = "\"" + value + "\"";
- }
- }
-
- if (value == "null")
- {
- tempArr = null;
- }
- else
- {
- tempArr = JsonConvert.DeserializeObject(value, property[i].PropertyType);
- }
- property[i].SetValue(t, tempArr, null);
- }
- else
- {
- UnityLog.LogError(property[i].Name + " is null!");
- }
- }
- catch (Exception ex)
- {
-
- UnityLog.LogError(ex.ToString() + typeof(T).Name + "--" + property[i].Name + " is error value");
- }
- }
- }
- }
- return t;
- }
- }
- }
|