1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
using JetBrains.Annotations;
using LitJson;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Reflection;
using UnityEngine;
using yutokun;
using static UnityEngine.Rendering.DebugUI;
public class CSVReader
{
private static Dictionary<string/*key*/, int/*index*/> m_KeyMapping = new Dictionary<string, int>();
private static List<List<string>> m_Rows = new List<List<string>>();
/// <summary>
/// 解析csv表格,并返回列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <returns></returns>
public static List<T> Read<T>(string content) where T : new()
{
List<T> result = new List<T>();
Read<T>(result, content);
return result;
}
/// <summary>
/// 解析csv表格,并返回数量
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <returns></returns>
public static int Read<T>(List<T> target, string content) where T : new()
{
m_KeyMapping.Clear();
m_Rows.Clear();
m_Rows = CSVParser.LoadFromString(content);
// 第一行是key
List<string> keys = m_Rows[0];
for (int i = 0; i < keys.Count; ++i)
{
m_KeyMapping.Add(keys[i], i);
}
int count = 0;
Type type = typeof(T);
for (int i = 1; i < m_Rows.Count; ++i)
{
if (m_Rows[i] == null || m_Rows[i].Count == 0) // 空行
continue;
bool isBlank = true;
m_Rows[i].ForEach(s => { if (!string.IsNullOrEmpty(s)) isBlank = false; });
if (isBlank)
continue;
if (m_Rows[i][0].Length > 0 && m_Rows[i][0][0] == '#') // 注释
continue;
List<string> 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)
{
Type fieldType = fieldInfo.FieldType;
if (fieldType.IsEnum) // 如果是枚举,先转成int
{
int value = int.Parse(row[index]);
fieldInfo.SetValue(obj, value);
}
else
{
fieldInfo.SetValue(obj, Convert.ChangeType(row[index], fieldInfo.FieldType));
}
}
}
target.Add(obj);
count++;
}
return count;
}
/// <summary>
/// 解析csv表格,并按key存储到字典里,返回数量
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="target"></param>
/// <param name="content"></param>
/// <param name="keyName"></param>
/// <returns></returns>
public static int ReadDictionary<TKey, TValue>(Dictionary<TKey, TValue> target, string content, string keyName) where TValue : new()
{
List<TValue> data = CSVReader.Read<TValue>(content);
if (data == null || data.Count == 0)
return 0;
Type type_key = typeof(TKey);
Type type_value = typeof(TValue);
FieldInfo field = type_value.GetField(keyName);
Type type_field = field.FieldType;
int count = 0;
for (int i = 0; i < data.Count; ++i)
{
TValue d = data[i];
TKey key = default(TKey);
if (type_key.IsEnum)
{
if(type_field == typeof(string))
{
key = (TKey)Enum.Parse(type_key, field.GetValue(d).ToString());
}
else if(type_field == typeof(int))
{
key = (TKey)field.GetValue(d);
}
}
else
{
key = (TKey)field.GetValue(d);
}
if (key == null)
{
LogHelper.LogError("CSVReader.ReadDictionary(): key is null");
continue;
}
target.Add(key, d);
count++;
}
return count;
}
}
|