When 方法在扩展方法中。流利验证
下面的代码中的 x
是 string?
类型,这是预期的,但是如果我将 Must
更改为 When
那么x
的类型为 T
public static IRuleBuilderOptions<T, string?> Example<T>(this IRuleBuilder<T, string?> rule, int maximumLength = 100)
{
return rule
.Must(x => x?.Length > 0);
}
我不明白为什么会发生这种情况。在简单的规则下,x
的类型是 string?
,但在扩展方法中它变成 T
,而行为相同的方法(I猜测)按预期工作
RuleFor(x => x.Line)
.NotEmpty()
.When(x => x.Line?.Length > 0);
in the code below x
is of type string?
which is expected, but if I change Must
to When
then x
is of type T
public static IRuleBuilderOptions<T, string?> Example<T>(this IRuleBuilder<T, string?> rule, int maximumLength = 100)
{
return rule
.Must(x => x?.Length > 0);
}
I can not figure why this is happening. On simple rules it works, x
is of type string?
, but in extension method it becomes T
, while methods that behave the same(I guess) work as expected
RuleFor(x => x.Line)
.NotEmpty()
.When(x => x.Line?.Length > 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现应该这样做
Found that is should be done like this