This commit is contained in:
azw 2024-05-18 20:45:45 +08:00
parent 75d01d40d5
commit 4f1193248d

View File

@ -10,6 +10,7 @@ using Newtonsoft;
namespace F6 namespace F6
{ {
public delegate bool TraverseCb<T>(T ele); public delegate bool TraverseCb<T>(T ele);
public interface IMetaTable public interface IMetaTable
@ -87,7 +88,7 @@ namespace F6
{ {
this.fileName = fileName; this.fileName = fileName;
this.primKey = primKey; this.primKey = primKey;
} }
public void Traverse(TraverseCb<T> cb) public void Traverse(TraverseCb<T> cb)
{ {
@ -104,7 +105,6 @@ namespace F6
{ {
if (this.rawList.Count > 0) if (this.rawList.Count > 0)
{ {
return this.rawList[new Random().Next(0, this.rawList.Count)]; return this.rawList[new Random().Next(0, this.rawList.Count)];
} }
return default(T); return default(T);
@ -114,11 +114,34 @@ namespace F6
{ {
try try
{ {
BaseMeta baseMeta = new BaseMeta();
{
Type tType = typeof(T);
Type tBaseType = tType.BaseType;
int fieldIdx = 0;
foreach(var f in tBaseType.GetFields())
{
string fieldName = f.Name;
var tAttrs = f.GetCustomAttributes(typeof(MetaTableFieldAttribute), false);
if (tAttrs.Length > 0)
{
var tAttr = (MetaTableFieldAttribute)tAttrs.First();
if (tAttr != null)
{
fieldName = tAttr.fieldName;
}
}
baseMeta.nameIdxHash.Add(f.Name, fieldIdx);
baseMeta.nameIdxHash.Add(fieldName, fieldIdx);
++fieldIdx;
}
}
string fileContent = File.ReadAllText(this.fileName); string fileContent = File.ReadAllText(this.fileName);
var dataSet = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(fileContent); var dataSet = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(fileContent);
foreach(var row in dataSet) foreach(var row in dataSet)
{ {
T t = new T(); T t = new T();
t._meta = baseMeta;
Type tType = typeof(T); Type tType = typeof(T);
Type tBaseType = tType.BaseType; Type tBaseType = tType.BaseType;
int fieldIdx = 0; int fieldIdx = 0;