编译器模糊调用错误-使用Func<>或Action的匿名方法和方法组
Action
Func<string>
.
Action
Func<string>
class Program{ static void Main(string[] args) { ClassWithSimpleMethods classWithSimpleMethods = new ClassWithSimpleMethods(); ClassWithDelegateMethods classWithDelegateMethods = new ClassWithDelegateMethods(); // These both compile (lambda syntax) classWithDelegateMethods.Method(() => classWithSimpleMethods.GetString()); classWithDelegateMethods.Method(() => classWithSimpleMethods.DoNothing()); // These also compile (method group with explicit cast) classWithDelegateMethods.Method((Func<string>)classWithSimpleMethods.GetString); classWithDelegateMethods.Method((Action)classWithSimpleMethods.DoNothing); // These both error with "Ambiguous invocation" (method group) classWithDelegateMethods.Method(classWithSimpleMethods.GetString); classWithDelegateMethods.Method(classWithSimpleMethods.DoNothing); }}class ClassWithDelegateMethods{ public void Method(Func<string> func) { /* do something */ } public void Method(Action action) { /* do something */ }}class ClassWithSimpleMethods{ public string GetString() { return ""; } public void DoNothing() { }}
相关分类