我有以下代码:参数操作<T1, T2, T3>其中T3可选配
public static MyMethod()
{
...Do something
ProtectedMethod(param1, param2);
...Do something
}
protected static void ProtectedMethod(IEnumerable<string> param1, string param2, int param3 = 1)
{
... Do something
}
可选参数3参数的注意。
现在有很多原因需要将MyMethod方法的代码提取到它自己的类中,但是我无法从中提取ProtectedMethod,因为所有继承自此类的类都需要保留更改小而孤立。所以我想我可以在新类中使用与ProtectedMethod具有相同签名的Action委托。
的问题是,如果我声明的委托是这样的:
protected readonly Action<IEnumerable<string>, string, int> m_ProtectedMethod;
提取的代码不会因为它说的方法只被调用带有两个参数的喜欢。
如果我声明,像这样的委托:
protected readonly Action<IEnumerable<string>, string> m_ProtectedMethod;
当我把它作为参数传递给新的班级不喜欢它是因为该方法被定义为具有三个参数而不是两个。
到目前为止,我曾想过要解决这个问题的唯一方法是创建ProtectedMethod的重载版本以消除可选参数。
这是唯一的选择,还是有另一种方式做它,因为现在首选的方法是有可选参数,而不是重载方法?
没有关系,但看起来类似的东西:http://stackoverflow.com/questions/707780/is-there-a-reasonable-approach-to-default-type-parameters-in-c-sharp-泛型 – nawfal