0
我正在使用FileHelpers DelimitedClassBuilder基於數據表中的元數據構建類。這樣我可以讀取元數據中已知的任何文件。我把它作爲一個流讀取並分別處理每條記錄。爲了進一步處理,我想將一些字段添加到數據中,然後將其序列化爲JSON。這是添加字段部分,不起作用。該流返回一個對象。我正在使用解決方法。將該對象序列化爲JSON,將其反序列化爲字典,添加字段,然後再次將其序列化爲JSON。我相信這可以以更有效的方式完成。我試圖將其轉換爲列表,但不起作用。FileHelpers DelimitedClassBuilder在讀取後添加字段
//build runtime class based on metadata
FD.DelimitedClassBuilder cb = new FD.DelimitedClassBuilder("ImportFile", "|");
cb.IgnoreFirstLines = 1;
foreach (DT.DataRow drMetadata in dtMetadata.Rows)
{
cb.AddField(drMetadata["EntityColumnName"].ToString(), typeof(string));
cb.LastField.FieldQuoted = true;
cb.LastField.QuoteMode = FH.QuoteMode.AlwaysQuoted;
cb.LastField.QuoteMultiline = FH.MultilineMode.AllowForBoth;
}
//create async filehelper engine for row by row processing
FH.FileHelperAsyncEngine fhe = new FH.FileHelperAsyncEngine(cb.CreateRecordClass());
using (fhe.BeginReadStream(file))
{
foreach(object record in fhe)
{
//convert object to list, doesn't work
//SG.List<object> blaat = (record as SG.IEnumerable<object>).Cast<object>().ToList();
//serialize record class to json
string json = JS.JsonConvert.SerializeObject(record, JS.Formatting.Indented);
//convert message to key-value dictionary
SG.IDictionary<string, string> values = JS.JsonConvert.DeserializeObject<SG.IDictionary<string, string>>(json);
values.Add("SourceSystem", messagesource);
values.Add("SourceType", messagetype);
string json2 = JS.JsonConvert.SerializeObject(values, JS.Formatting.Indented);
SY.Console.WriteLine(json2);
}
}
fhe.Close();