Whe*_*lie 6 c# resharper attributes generic-type-argument
ReSharper 具有一套代码注释,可用于显式表达 IDE 可以使用的代码意图。最有用的两个注释是[CanBeNull]和[NotNull]属性,它们可用于构造函数、属性和方法,如下所示:
[CanBeNull]
private Foo DoSomething([NotNull] string text)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是一个远景,但是有什么方法可以将这些属性分配给 Action 或 Func 参数吗?
我知道以下代码是非法的(因为类型参数不是 attribute 的有效目标),但是是否有其他表达方式?
private void DoSomething(Action<[NotNull]string> processText)
{
///...
}
Run Code Online (Sandbox Code Playgroud)
如果您愿意创建自定义委托类型,则可以执行此操作:
delegate void TextProcessor([NotNull] string text);
delegate void NullableTextProcessor([CanBeNull] string text);
private void DoSomething([NotNull] TextProcessor processText)
{
// ...
}
private void DoSomethingNull([NotNull] NullableTextProcessor processText)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,CanBeNull 似乎没有在 lambda 语法中给出警告:
但您可能只想等待 C# 8 的可为空/不可为空引用类型。
| 归档时间: |
|
| 查看次数: |
1339 次 |
| 最近记录: |