输入类型在 lambda 表达式中自动更改
Input type changes automatically inside lambda expression
我在使用 Fluent Validation Must
子句时注意到了一个很奇怪的问题。
假设我有这样的模型:
public class PhoneDetail
{
public int PrefixId { get; set; }
public string Digits { get; set; }
}
这是验证器:
public PhoneDetailValidator()
{
this.RuleFor(phone => phone.Digits)
.Must(x => x == null);
}
你认为是什么类型x
? String
?是的,你是对的。
但是,那这是什么?
它的类型已从 string
更改为 PhoneDetail
。
我正在使用 Visual Studio 2012.
是 bug 还是什么?
更新:
此外,值得一提的是,在第一种情况下它编译得很好。但是,在第二种情况下发生编译器错误:
> Delegate 'System.Func<SportsStore.WebUI.Models.PhoneDetail,string,bool>'
> does not take 1 arguments
它要么是一个 IDE 错误,要么它试图通过向您展示 Must
方法的另一个重载的 IntelliSense 来变得聪明。其他重载之一采用略有不同的 Func
,其中包括根对象,因此您可以编写:
this.RuleFor(phone => phone.Digits)
.Must((phone, x) => phone.Digits == null);
我在使用 Fluent Validation Must
子句时注意到了一个很奇怪的问题。
假设我有这样的模型:
public class PhoneDetail
{
public int PrefixId { get; set; }
public string Digits { get; set; }
}
这是验证器:
public PhoneDetailValidator()
{
this.RuleFor(phone => phone.Digits)
.Must(x => x == null);
}
你认为是什么类型x
? String
?是的,你是对的。
但是,那这是什么?
它的类型已从 string
更改为 PhoneDetail
。
我正在使用 Visual Studio 2012.
是 bug 还是什么?
更新:
此外,值得一提的是,在第一种情况下它编译得很好。但是,在第二种情况下发生编译器错误:
> Delegate 'System.Func<SportsStore.WebUI.Models.PhoneDetail,string,bool>'
> does not take 1 arguments
它要么是一个 IDE 错误,要么它试图通过向您展示 Must
方法的另一个重载的 IntelliSense 来变得聪明。其他重载之一采用略有不同的 Func
,其中包括根对象,因此您可以编写:
this.RuleFor(phone => phone.Digits)
.Must((phone, x) => phone.Digits == null);