Ho una classe denominata Test
che ha un constructor
per accettare Action<T>
e l'altra accetta Func<T,T>
. Si prega di consultare il frammento di seguito.Errore di chiamata del costruttore ambiguo
public class Test<T>
{
//constructors
public Test() { }
public Test(Action<T> action) { }
public Test(Func<T, T> action) { }
//methods with same signature of constructor
public void MyMethod1(Action<T> action) { }
public void MyMethod2(Func<T, T> action) { }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Test<string> t1 = new Test<string>(this.MyMethod1);
Test<string> t2 = new Test<string>(this.MyMethod2);
Test<string> t = new Test<string>();
t.MyMethod1(MyMethod1);
t.MyMethod2(MyMethod2);
}
public void MyMethod1(string value) { }
public string MyMethod2(string value) { return string.Empty; }
}
Ma al di sotto di linee getta una chiamata ambigua errore
Test<string> t1 = new Test<string>(this.MyMethod1);
Test<string> t2 = new Test<string>(this.MyMethod2);
e il punto interessante è, ho due metodi con la stessa firma del mio Test
classe constructor
che non gettare alcun errore ambiguo
Test<string> t = new Test<string>();
t.MyMethod1(MyMethod1);
t.MyMethod2(MyMethod2);
Qualcuno potrebbe aiutarmi a identificare e risolvere il problema.
chiarimento: è la domanda "perché fare le assegnazioni non compilare "o" perché il metodo chiama compilare quando i compiti non lo fanno "? – Jon
Qualcuno con il distintivo d'oro chiudi questo come duplicato di [questo] (http://stackoverflow.com/questions/2057146/compiler-ambiguous-invocation-error-anonymous-method-and-method-group-with-fun) per favore. Riapido per modificare il duplicato suggerito ma non sono autorizzato a farlo :( –