0

I have a model that the API backend is expected that period could ethier be a string like "today","yesterday",etc

Or it could be a List<int> like "[9292,8383]","[8891,9333]"

internal class CallHistoryModel
    {
        public string period { get; set; }
    }

Similar to how you can create a method override using the same name with different parameters is it possible to create either a class override or variable override.

Keeping the same name is important because the class is converted to json when it is sent to the server

To provide more context I know that I can do something like

public class CallHistoryModel<T>
    {
        public T period { get; set; }

        public CallHistoryModel(T _period)
        {
            period = _period;
        }
    }

However period is not a required parameter that will always be sent and my question is wondering if there is a better alternative that the user would not have to specify the type unless it is being used

Joséph Flames
  • 204
  • 1
  • 12
  • 1
    Make it generic? – ChiefTwoPencils Dec 15 '21 at 21:01
  • @ChiefTwoPencils That would work but would not be the most elegant solution since period is not always required so having to specify the type when initializing would not be optimal was hoping there was a better way – Joséph Flames Dec 15 '21 at 21:06
  • 2
    Could you just make it an `object`? And expect consuming code to perform a cast when they know what type it has? – StriplingWarrior Dec 15 '21 at 21:14
  • Given that this is a DTO ("the class is converted to json when it is sent to the server"), it might be best to explicitly make it a JToken, and then allow that to be converted wherever server-side code knows what it actually is. – StriplingWarrior Dec 15 '21 at 21:16
  • 1
    @StriplingWarrior Brilliant! Can't believe I didn't think to just specify it as an object – Joséph Flames Dec 15 '21 at 21:29

0 Answers0