Issue:
I have an object with properties that models the hours of the day. I would like to know if it's possible to index the properties of this object similar to how they are indexed in an array or dictionary and then get these values by their index.
Example:
internal class HoursOfDay
{
[JsonProperty("hour00", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour00 { get; set; }
[JsonProperty("hour01", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour01 { get; set; }
[JsonProperty("hour02", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour02 { get; set; }
[JsonProperty("hour03", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour03 { get; set; }
[JsonProperty("hour04", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour04 { get; set; }
[JsonProperty("hour05", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour05 { get; set; }
[JsonProperty("hour06", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour06 { get; set; }
[JsonProperty("hour07", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour07 { get; set; }
[JsonProperty("hour08", NullValueHandling = NullValueHandling.Ignore)]
public HourDetailsExample Hour08 { get; set; }
...
I would like to be able to access the properties of this object like this if possible:
var hoursOfDay = new HoursOfDay();
var h1 = hoursOfDay["0"] // or hoursOfDay[0] or hoursOfDay["hour00"]
I understand I could just turn this object into an array or a dictionary but I was just curious if indexing on the properties of an object is possible.
Edit I also understand this is something I can do with reflection. I want to know if I can do with indexing.
Edit2 Similarly, I also would like to know how to update the setter: example:
var h1[0] = new HourDetailsExample("exampleData");
** Edit3** I've learned this can be added by adjusting the below answers to add a setter like so:
set
{
switch (index)
{
case 0:
Hour00 = value; break;
case 1:
Hour01 = value; break;
...
default: break;
}
}