I have a project with around 100k XML configuration files for managing test equipment. In order to make the files easier to update, I want to be able to port them around to Excel, Access, etc. I've written VBA code to do this, but it is painfully slow. C# is orders of magnitude faster and offers more utility. I created XML Schema to read the XML, but now I want to create a set of reusable data objects that manage the reading and writing to various formats and derived objects that specify the data content.
I decided to build a two-tier data structure, but the static constructors don't seem to perform as one would expect, and I need a workaround.
public abstract class Data_Structure {
private static readonly Dictionary<string, int> _DatIndex = new Dictionary<string, int>();
private static string _Name = "";
private string[] _Data = null;
protected static void Init(string datName, List<string> listProps) {
int iIndex = 1;
_Name = datName;
foreach (string iProp in listProps) {
_DatIndex.Add(iProp, iIndex);
iIndex++;
}
}
public Data_Structure() {
_Data = new string[_DatIndex.Count + 1];
}
public static Dictionary<string, int> Get_PropList => _DatIndex;
public static string Get_Name => _Name;
public string Prop_Get(string Prop_Name) {
return this._Data[_DatIndex[Prop_Name]];
}
public void Prop_Set(string Prop_Name, string Prop_Value) {
this._Data[_DatIndex[Prop_Name]] = Prop_Value);
}
// Code to manage input/output
}
public class Data_Item : Data_Structure {
static Data_Item() {
List<string> listProps = new List<string>();
PropertyInfo[] arryProps = typeof(Data_Item).GetProperties();
foreach (PropertyInfo iProp in arryProps) {
listProps.Add(iProp.Name);
}
Init("Data_Item_Name", listProps);
}
public string Property1 {
get => this.Prop_Get("Property1");
set => this.Prop_Set("Property1", value);
}
// More Properties...
}
The idea is that Data_Structure
can be the interface between all the different I/O formats. For each new XML file type, I will create a child instance (i.e. Data_Item
) that defines its properties. I want to do it this way because I can use things like BindingList<T>
, etc.
When I go to use the code, I try and pull the property list like so:
public void testFunction() {
Console.WriteLine(Data_Item.Get_PropList.Count); //Outputs 0
Data_Item tempItem = new Data_Item();
Console.WriteLine(Data_Item.Get_PropList.Count); //Outputs correct number of properties
}
I'm not too familiar with reflection, but I understand its really slow during execution. In the above code, the intent is to front-load the reflection parts so at runtime (when iterating across 100k files) it executes faster. Also, any suggestions to make the derived classes simpler to define would be much appreciated. Thank you!
I am using C# 7.3 due to limitations on my computer. I am also not able to use Linq.
Aucun commentaire:
Enregistrer un commentaire