.Net 参数属性使命名/可选参数仅在内部/私有可见?
出于好奇,有没有一种方法可以编写这样的方法:
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# 编译器在这种情况下可以生成第二种内部方法会很好。
这已经有可能了吗?到目前为止找不到任何东西。
Out of curiosity, is there a way to write a method e.g. like this:
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);
}
I want to cut down code redundancies internally, but remain compliant to e.g. BCL method signatures for (Try)Parse() but if the c# compiler could in this case generate a second, internal method that would be nice.
Is that already somehow possible? Couldn't find anything so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你可以,但这不会给你同样的结果吗?
I'm not aware that you can, but wouldn't this give you the same result?
我知道这个答案有点晚了,但可能对其他人有帮助。
您可以使用 AttributeTargets.Parameter 装饰您的属性类(此处是 msdn 链接),这正是您正在寻找的内容。
属性示例:
属性的用法:
I know this is a bit late answer, but it might be of help to somebody else.
You can decoreate your attribute class with AttributeTargets.Parameter (here is the msdn link) which is exactly what you are looking for.
A sample attribute:
Usage of the attribute: