2017-10-08 2 views
0

Je veux créer un tuple en utilisant Tuple.Create() avec la signature de type de Tuple<String,String,Func<String,Control>>Créer Tuple avec Multi paramètre Func

, mais quand je fais; J'obtiens l'erreur:

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

Voici mon extrait de code:

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 
} 

Je veux le faire en utilisant Tuple.Create() est-il possible sans new Tuple<T1,T2,T3>(T1,T1,T3)

Répondre

1

Vous devez spécifier explicitement le type du dernier paramètre par soit en fournissant les paramètres de type:

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) 
}; 

ou en passant un 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)) 
}; 

Plus d'informations sur le pourquoi:

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