我有一个带有重载方法的类:
MyClass.DoThis(Action<Foo> action);
MyClass.DoThis(Action<Bar> action);
Run Code Online (Sandbox Code Playgroud)
我想将lambda表达式传递给Action版本:
MyClass.DoThis( foo => foo.DoSomething() );
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于围绕"foo"变量的类型推断,Visual Studio无法区分Action<Foo>和Action<Bar>版本之间的区别- 因此它引发了编译器错误:
以下方法或属性之间的调用不明确:'MyClass.DoThis(System.Action
<Foo>)'和'MyClass.DoThis(System.Action<Bar>)'
什么是最好的解决方法?
Mar*_*ell 23
MyClass.DoThis((Foo foo) => foo.DoSomething());
Run Code Online (Sandbox Code Playgroud)