*Say I've got a class with a constructor with one required parameter and three optional ones.
public class myClass(int reqInt, string optStr1 = "st1", uint optUint1 = 0, float optFloat =0.0)
{ ... }
I know that, in my calling code, I can effectively treat this as an overloaded function, e.g.
myClass Foo = new myClass(1, "test");
myClass Foo2 = new myClass(2, "hellow world", 4);
and even
myClass Foo3 = new myClass(3, optFloat : 4.5)
What I can't figure out is how to build my parameters tuple dynamically. E.g. say my data is coming from a JSON, and I might get the following array:
[
{ "myInt" : "1"},
{"myInt :"2", "myString" : "word"},
{"myFloat" : "4.5", "myString" : "to" , "myInt" : "3"},
{"myUInt" : "4", "myInt" "-4", "myFloat" : "4.4", "myString" : "yo!"}
]
I can obviously create 8 different callers*, but that seems like it just moves the overload from the constructor to the caller. But I can't figure out how to dynamically build the tuple, especially using named parameters/aliases, so I only have 1 calling function.
An arraylist seems like it would work, but (I think) I need to use named parameters, since there will be cases where I don't have optional parameter 1, but do have 2 or 3 (or any combinations thereof).
This can't be an edge case, but I'm not finding anything that works in my searches (I don't work in C#, or really any strongly typed languages most of the time, so forgive me if this is a really basic question).
*meaning, assume I've deserialized my Json into myJson , I could do:
If(myJson.myString.hasValue){
if(myJson.myString.hasValue){
if(myJson.myUInt.hasValue){
if(myJson.myFloat.hasValue){
(int, string, uint, float) myParams = (reqint: myJson.myInt, optStr1: myJson.myString, optUint1: myJson.myUInt, optFloat: myJson.myFloat)
} else if
...
and so on, recursively building up the 8 possible cases. But that seems really verbose...