我正在使用 VS2008 SP1,WCF Ria Service 2009 年 7 月 CTP。我发现 MetadataType 在部分类模式下不起作用,真的不知道我错过了什么:
工作:-
public partial class Person
{
private string _Name;
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}
不行
public partial class Person
{
private string _Name;
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}
public partial class PersonMetadata
{
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name;
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}