I'm currently making instances of my classes from a generic csv reader(see source here). It checks what properties a class has and which order they are in, and then assigns them in that order from the csv file given.
My issue is that I have a class that needs to be able to take an object property, since it needs to be able to be different types of classes(One instance of the class may contain a Foo class instance as property value while another has a Bar class instance as that property value). The reader doesn't know a string from an object though, and I can't place the separation character within the objects place in the csv. However, all classes have a Name property(string).
Could I somehow use that as a key to search all object instances for a key match?
An example of the class that should hold another class:
public class ClassHolder : CsvableBase
{
public ClassHolder() { }
public ClassHolder(string name, object obj)
{
Name = name;
Obj = obj;
}
public string Name { get; set; }
public object Obj { get; set; }
}
public class Foo : CsvableBase
{
public Foo() { }
public Foo(string name, int amount)
{
Name = name;
Amount = amount;
}
public string Name { get; set; }
public int Amount { get; set; }
}
In the Csv file it's properties would be written like this:
Name;Obj
ClassHolderName;FooName FooAmount
So the name for ClassHolder is correct, but the Obj property is the string "FooName FooAmount"
If this is not possible I'd love suggestions for another approach, I'm new to programming, and the more ways to approach a problem I know the better.
CsvableBase
-- but mind you don't break garbage collection by keeping references to everything forever). Is CSV an absolute requrement here? Because what you want is JSON or XML. – 15ee8f99-57ff-4f92-890c-b56153