*假设我有一个类,它的构造函数有一个必需参数和三个可选参数。
public class myClass(int reqInt, string optStr1 = "st1", uint optUint1 = 0, float optFloat =0.0)
{ ... }
myClass Foo = new myClass(1, "test");
myClass Foo2 = new myClass(2, "hellow world", 4);
甚至
myClass Foo3 = new myClass(3, optFloat : 4.5)
我不知道的是如何动态地构建参数元组。E、 g.假设我的数据来自JSON,我可能会得到以下数组:
[
{ "myInt" : "1"},
{"myInt :"2", "myString" : "word"},
{"myFloat" : "4.5", "myString" : "to" , "myInt" : "3"},
{"myUInt" : "4", "myInt" "-4", "myFloat" : "4.4", "myString" : "yo!"}
]
arraylist似乎可以工作,但是(我认为)我需要使用命名参数,因为有时我没有可选的参数1,但是有2或3(或任意组合)。
*意思是,假设我已经将我的Json反序列化为
myJson
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
...
以此类推,递归地建立8个可能的案例。但这似乎很冗长。。。