我正在使用VS2008 SP1,WCF Ria服務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();
}
}
}
我似乎有這個問題,直到我一對System.ComponentModel.DataAnnotations命名空間的引用。 – 2010-07-01 20:57:30