MetadataType问题问题、MetadataType

2023-09-02 10:33:10 作者:沙哑

我使用VS2008 SP1中,WCF RIA服务2009年7月CTP。我发现MetadataType不能在局部类模式下工作,真不知道我错过了:

工作: -

 公共部分类人
{
    私人字符串_Name;

    [必需的(AllowEmptyStrings =假,的ErrorMessage =名称必需输入)
    [StringLength(3)]
    公共字符串名称
    {
        集合{_Name =值;}
        {返回_Name;}
    }
}

类节目
{
    静态无效的主要(字串[] args)
    {
        者P =新的Person {名称=123432};
        名单解析度=新名单();
        Validator.TryValidateObject(P,新ValidationContext(P,NULL,NULL)
            水库,真正的);
        如果(res.Count大于0)
        {
            Console.WriteLine(水库[0] .ErrorMessage);
            到Console.ReadLine();
        }
    }
}
 

无法正常工作

 公共部分类人
{
    私人字符串_Name;

    公共字符串名称
    {
        集合{_Name =值;}
        {返回_Name;}
    }
}

[MetadataType(typeof运算(PersonMetadata))]
公共部分类人
{
}


公共部分类PersonMetadata
{
    [必需的(AllowEmptyStrings =假,的ErrorMessage =名称必需输入)
    [StringLength(3)]
    公共字符串名称;
}

类节目
{
    静态无效的主要(字串[] args)
    {
        者P =新的Person {名称=123432};
        名单解析度=新名单();
        Validator.TryValidateObject(P,新ValidationContext(P,NULL,NULL)
            水库,真正的);
        如果(res.Count大于0)
        {
            Console.WriteLine(水库[0] .ErrorMessage);
            到Console.ReadLine();
        }
    }
}
 
mathtype6.9 添加到 Word2016 中 解决Word快速选项卡中两个mathtype问题

解决方案

编辑:我在这里找到了答案:http://forums.silverlight.net/forums/p/149264/377212.aspx

在验证,则需要手动注册的元数据类:

  TypeDescriptor.AddProviderTransparent(
            新AssociatedMetadataTypeTypeDescriptionProvider(typeof运算(人)的typeof(PersonMetadata))的typeof(人));

        名单<为ValidationResult> RES =新的名单,其中,为ValidationResult>();
        布尔有效= Validator.TryValidateObject(P,新ValidationContext(P,NULL,NULL),资源,真正的);
 

(原来的答复如下)的

现在的问题是没有具体与您的局部类,那就是Validator.TryValidateObject似乎并没有认识到MetaDataType属性。我有同样的问题 - 在MVC 2内置的验证识别的元数据类,但TryValidateObject不

请参阅以下: http://stackoverflow.com/questions/2050161/validating-dataannotations-with-validator-class http://stackoverflow.com/questions/2422031/validation-does-not-work-when-i-use-validator-tryvalidateobject

作为一个方面说明,我不知道这是否是必要的,但我已经看到了元数据类的所有实例使用默认的get /每个属性设置:

  [必需的(AllowEmptyStrings =假,的ErrorMessage =名称必需输入)
[StringLength(3)]
公共字符串名称{;组; }
 

I'm using VS2008 SP1, WCF Ria Service July 2009 CTP. I found out that MetadataType does not work in partial class mode, really don't know what I have missed out:

Work:-

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();
        }
    }
}

Not Work

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();
        }
    }
}

解决方案

EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx

Before validating, you need to manually register the metadata class:

TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person));

        List<ValidationResult> res = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true);

(Original answer follows)

The problem isn't specifically with your partial class, it's that Validator.TryValidateObject doesn't seem to recognize the MetaDataType attribute. I have the same problem - the built-in validation in MVC 2 recognizes the metadata class, but TryValidateObject doesn't.

See these: http://stackoverflow.com/questions/2050161/validating-dataannotations-with-validator-class http://stackoverflow.com/questions/2422031/validation-does-not-work-when-i-use-validator-tryvalidateobject

As a side note, I don't know if it's necessary, but all examples I've seen for metadata classes employ the default get/set on each property:

[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name { get; set; }