出于好奇,有没有办法编写一个方法,例如像这样:
public static MyType Parse(string stringRepresentation, [Internal] bool throwException = true)
{
// parsing logic here that conditionally throws an exception or returns null ...
}
public static MyType TryParse(string stringRepresentation)
{
return this.Parse(stringRepresentation, true);
}
我想减少内部代码冗余,但仍遵守例如(Try)Parse() 的 BCL 方法签名,但如果 c# 编译器可以在这种情况下生成第二个内部方法,那就太好了。
这已经有可能了吗?目前还没有找到任何东西。
最佳答案
我不知道你可以,但这不会给你同样的结果吗?
public MyType Parse(string stringRepresentation)
{
return this.Parse(stringRepresentation, true);
}
internal MyType Parse(string stringRepresentation, bool throwException = true)
{
// parsing logic here that conditionally throws an exception or returns null ...
}
关于.Net 参数属性使命名/可选参数仅在内部/私有(private)可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4584576/