using JetBrains.Annotations; using System; using System.Collections; using System.Collections.Generic; using System.Net.Mime; using System.Reflection; using UnityEngine; using yutokun; using static UnityEngine.Rendering.DebugUI; namespace WK.Data { public class CSVReader { private static Dictionary m_KeyMapping = new Dictionary(); private static List> m_Rows = new List>(); public static List Read(string content) where T : new() { m_KeyMapping.Clear(); m_Rows.Clear(); m_Rows = CSVParser.LoadFromString(content); // µÚÒ»ÐÐÊÇkey List keys = m_Rows[0]; for (int i = 0; i < keys.Count; ++i) { m_KeyMapping.Add(keys[i], i); } List result = new List(); Type type = typeof(T); for (int i = 1; i < m_Rows.Count; ++i) { if (m_Rows[i][0][0] == '#') // ×¢ÊÍ continue; List row = m_Rows[i]; T obj = new T(); foreach(var key in m_KeyMapping) { int index = key.Value; var fieldInfo = type.GetField(key.Key); if(fieldInfo != null) { fieldInfo.SetValue(obj, Convert.ChangeType(row[index], fieldInfo.FieldType)); } } result.Add(obj); } return result; } } }