I am trying to write a simple role playing game in C# to become more familiar with the language.
I have a class that loads data from CSV file, creates an object, and places it in a dictionary. Because every aspect of the game has different data (items, actors, skills, etc), I have set up each of these as a class, but this requires me to re-implement a Load() method for each one. After doing this 5 or 6 times, I am wondering if there isn't a better way to implement this.
Basically, what I would want to do is parse over the first line of the CSV which contains headers, and use these as class variable names. Currently, they are implemented as a dictionary relationship, so I would do SomeClassInstance.dict["id"], where I would ideally type SomeClassInstance.id, which is entirely generated from the contents of the file.
Is that a thing? How do I do this?
If you stick to your current design (CSV + dictionary) you could use the ExpandoObject class to get what you are looking for, create a simple factory class:
public static class ObjectFactory
{
public static dynamic CreateInstance(Dictionary<string, string> objectFromFile)
{
dynamic instance = new ExpandoObject();
var instanceDict = (IDictionary<string, object>)instance;
foreach (var pair in objectFromFile)
{
instanceDict.Add(pair.Key, pair.Value);
}
return instance;
}
}
This factory will create an object instance of whatever dictionary you give it, i.e. just one method to create all your different kinds of objects. Use it like this:
// Simulating load of dictionary from file
var actorFromFile = new Dictionary<string, string>();
actorFromFile.Add("Id", "1");
actorFromFile.Add("Age", "37");
actorFromFile.Add("Name", "Angelina Jolie");
// Instantiate dynamically
dynamic actor = ObjectFactory.CreateInstance(actorFromFile);
// Test using properties
Console.WriteLine("Actor.Id = " + actor.Id +
" Name = " + actor.Name +
" Age = " + actor.Age);
Console.ReadLine();
Hopes this helps. (And yes she was born 1975)
You need to read up on serialization - instead of holding the files in CSV files, you can store them in a serialized format and load them directly into the wanted type.
You will only need a couple of methods to serialize and deserialize.
I suggest reading up on:
The above links are to different serializers (and I have certainly left some off - anyone in the know, if there is a good serializer that you know, please add) - read through, see their APIs, play around with them and see their on-disk formats and make your decision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With