I have a feed coming which has different properties serialized as comma separated values
feed = "[{"HIGH", "[235.76, 235.96, 235.97]"},
{"LOW", "[235.76, 235.96, 235.97'"},
{"DATE", "[20170410-10:21:34, 20170410-10:31:34, 20170410-10:43:34, 20170410-10:59:34]" } .....
]"
I have a business need to create an entity out of this containing different properties for each of HIGH /LOW /Date etc. Now "High" and Low will be of double whereas Date will be of dateTime type. I am splitting the string following each tag and then parsing each value to the corresponding property.
However since the property's datatype differ, I require a parse function for each type.
private void GetFeedData(string[] data, List<int> field)
{
for (int i = 0; i < data.Length; i++)
{
**int fieldValue = int.Parse(data[i]);**
field.Add(fieldValue);
}
}
I have ended up with different functions, one specific to each datatype, differing in just the parsing of fieldValue
I am want to do something like this, but this doesn't seem to be supported.
private void GetFeedData<T>(string[] data, List<T> field) where T: struct
{
for (int i = 0; i < data.Length; i++)
{
T fieldValue = (T)(data[i]);
field.Add(fieldValue);
}
}
This will be a single method for all types, which will be a lot easier. Not sure how to achieve this.. Please help
Try this, but I think your example is not syntactically correct.
private IList<T> GetFeedData<T>(string[] data)
{
List<T> field = new List<T>();
for (int i = 0; i < data.Length; i++)
{
T fieldValue = (T)(Convert.ChangeType(data[i], typeof(T)));
field.Add(fieldValue);
}
return field;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.