最近,我想在扩展方法中添加一个可选参数.原始方法看起来像这样:
public static class Extensions {
public static bool Foo(this IFoo target) {
target.DoIt(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这显然是一个简化版本,但让我们继续.
我做的是:
public static class Extensions {
public static bool Foo(this IFoo target, bool flag = true) {
target.DoIt(flag);
}
}
Run Code Online (Sandbox Code Playgroud)
我所做的就是引入一个带有默认值的可选参数,对吧?我期待的是编译器生成重载方法,没有标志.这部分发生了.我重新编译的任何代码都能够编译和执行而没有任何问题,即使没有像这样的参数:
...
IFoo foo = new FooBar();
foo.Foo();
...
Run Code Online (Sandbox Code Playgroud)
但是,任何针对以前版本的Foo()构建的代码都不起作用,抛出以下异常:
Unhandled Exception: System.MissingMethodException: Method not found: 'Boolean Models.Class1.Foo()'.
at DefaultParamsTests.Program.Main(String[] args)
Run Code Online (Sandbox Code Playgroud)
这对我们来说显然是一个问题,因为我们有一个公共API,我们的客户可以利用它,这将是一个突破性的变化.
解决方案是明确创建一个重载:
public static class Extensions {
public static bool Foo(this IFoo target) {
target.DoIt(true);
}
public static bool Foo(this IFoo target, …Run Code Online (Sandbox Code Playgroud) c# ×1