Browse Source

fixed bugs csv-GetAll

master
jkpete 2 months ago
parent
commit
0804297eed
  1. 25
      Example/UsingTest/Script/EGSaveTest.cs
  2. 4
      addons/EGFramework/Module/SaveTools/EGCsvSave.cs

25
Example/UsingTest/Script/EGSaveTest.cs

@ -25,12 +25,25 @@ namespace EGFramework.Examples.Test{
csvSave.InitSaveFile("SaveData/TestCsv.csv"); csvSave.InitSaveFile("SaveData/TestCsv.csv");
// Customer testData = csvSave.GetData<Customer>("",1); // Customer testData = csvSave.GetData<Customer>("",1);
// GD.Print("Name = "+testData.Name +" || ID = "+testData.Id); // GD.Print("Name = "+testData.Name +" || ID = "+testData.Id);
Customer testData = new Customer(){ // Customer testData = new Customer(){
Id = 1008, // Id = 1008,
Name = "AddDataDefault", // Name = "AddDataDefault",
IsActive = true // IsActive = true
}; // };
csvSave.SetData("",testData,2); // csvSave.SetData("",testData,2)
IEnumerable<Customer> allResult = csvSave.GetAll<Customer>("");
GD.Print("Get result " + allResult.Count());
foreach(Customer customer in allResult){
GD.Print(customer.Id +"|" + customer.Name);
}
// System.Linq.Expressions.Expression<Func<Customer, bool>> expr = i => i.Id < 5;
// IEnumerable<Customer> linqResult = csvSave.FindData<Customer>("",expr);
// GD.Print("Find result " + linqResult.Count());
// foreach(Customer customer in linqResult){
// GD.Print(customer.Name);
// }
// GD.Print(typeof(Customer)); // GD.Print(typeof(Customer));
// Type type = typeof(Customer); // Type type = typeof(Customer);

4
addons/EGFramework/Module/SaveTools/EGCsvSave.cs

@ -165,19 +165,19 @@ namespace EGFramework
// throw new NotImplementedException(); // throw new NotImplementedException();
List<TData> DataList = new List<TData>(); List<TData> DataList = new List<TData>();
for (int dataID = 0; dataID < CsvDataBlock.Count(); dataID++){ for (int dataID = 0; dataID < CsvDataBlock.Count(); dataID++){
TData data = new TData();
foreach(PropertyInfo property in typeof(TData).GetProperties()){ foreach(PropertyInfo property in typeof(TData).GetProperties()){
CsvParamAttribute csvParam = property.GetCustomAttribute<CsvParamAttribute>(); CsvParamAttribute csvParam = property.GetCustomAttribute<CsvParamAttribute>();
if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){
string valueStr = CsvDataBlock[dataID][CsvDataHeader[csvParam._name]]; string valueStr = CsvDataBlock[dataID][CsvDataHeader[csvParam._name]];
TData data = new TData();
if(property.PropertyType==typeof(string)){ if(property.PropertyType==typeof(string)){
property.SetValue(data,valueStr); property.SetValue(data,valueStr);
}else{ }else{
property.SetValue(data,Convert.ChangeType(valueStr,property.PropertyType)); property.SetValue(data,Convert.ChangeType(valueStr,property.PropertyType));
} }
DataList.Add(data);
} }
} }
DataList.Add(data);
} }
TypeDataContainer.Register(DataList); TypeDataContainer.Register(DataList);
return DataList; return DataList;

Loading…
Cancel
Save