I'm working on an API which will read string values from a file and allow for type conversion. I also need it to provide a way of "mapping" given string values to a specific value of the desired type. That would look something like this:
int val = myObject.getValue<int>("FieldName", { { "", 0 }, { "INF", int.MaxValue } });
The field name is there so that it can be retrieved later if an error occurs. It's only important to this example because the function needs to take an unrelated first parameter.
However, I am having trouble coming up with an elegant and type-safe syntax to provide this functionality (the above, based on collection initializers, only works if I stick new FieldValueMappingCollection<int> { ... }
in there).
So, the options I'm aware of are:
myObject.getValue<int>("FieldName", new FieldValueMappingCollection<int> { { "", 0 }, { "INF", int.MaxValue } });
or
myObject.getValue<int>("FieldName", "", 0, "INF", int.MaxValue);
where getValue has params object[]
in its signature.
There's got to be a nicer way of doing this, right?