2017-10-08 84 views
0

我想創建一個使用Tuple.Create()Tuple<String,String,Func<String,Control>>與多參數功能建立的元組

類型簽名的元組,但是當我做;我得到的錯誤:

The type arguments for method 'Tuple.Create<T1,T2,T3>(T1,T2,T3)' 
cannot be inferred from the usage. Try specifying the types explicitly. 

這裏是我的代碼片段:

public List<Tuple<String, String, Func<string,Control>>> Headers { get; set; } = new List<Tuple<String, String, Func<string,Control>>> { 
      Tuple.Create("Name","Type", TypeControl), 
      Tuple.Create("Age","TypeAge", AgeControl), 
     }; 

public Control TypeControl(string data = ""){ 
// code returns a Control 
} 
public Control AgeControl(string data = ""){ 
// code returns a Control 
} 

我要做到這一點使用Tuple.Create()是有可能不new Tuple<T1,T2,T3>(T1,T1,T3)

回答

1

你必須通過顯式地指定最後一個參數的類型提供類型參數:

public List<Tuple<string, string, Func<string, Control>>> Headers { get; set; } = new List<Tuple<string, string, Func<string, Control>>> { 
    Tuple.Create<string, string, Func<string, Control>>("Name","Type", TypeControl), 
    Tuple.Create<string, string, Func<string, Control>>("Age","TypeAge", AgeControl) 
}; 

或傳遞一個Func<string, Control>:關於爲什麼

public List<Tuple<string, string, Func<string, Control>>> Headers { get; set; } = new List<Tuple<string, string, Func<string, Control>>> { 
    Tuple.Create("Name","Type", new Func<string, Control>(TypeControl)), 
    Tuple.Create("Age","TypeAge", new Func<string, Control>(AgeControl)) 
}; 

的更多信息:

Why can't C# compiler infer generic-type delegate from function signature?