【问题标题】:.NET 4 RTM MetadataType attribute ignored when using Validator.NET 4 RTM MetadataType 属性在使用验证器时被忽略
【发布时间】:2011-02-09 02:02:14
【问题描述】:

我正在使用 VS 2010 RTM 并尝试使用 MetadataTypeAttribute 对简单类型执行一些基本验证。当我将验证属性放在主类上时,一切正常。但是,当我把它放在元数据类上时,它似乎被忽略了。我一定错过了一些微不足道的事情,但我已经坚持了一段时间了。

我查看了企业库验证块作为一种解决方法,但它不支持开箱即用的单个属性的验证。有什么想法吗?

class Program
{
    static void Main(string[] args)
    {
        Stuff t = new Stuff();

        try
        {
            Validator.ValidateProperty(t.X, new ValidationContext(t, null, null) { MemberName = "X" });
            Console.WriteLine("Failed!");
        }
        catch (ValidationException)
        {
            Console.WriteLine("Succeeded!");
        }
    }
}

[MetadataType(typeof(StuffMetadata))]
public class Stuff
{
    //[Required]  //works here
    public string X { get; set; }
}

public class StuffMetadata
{
    [Required]  //no effect here
    public string X { get; set; }
}

【问题讨论】:

    标签: c# .net validation .net-4.0 metadatatype


    【解决方案1】:

    Validator 似乎不尊重 MetadataTypeAttribute:

    http://forums.silverlight.net/forums/p/149264/377212.aspx

    关系必须显式注册:

     TypeDescriptor.AddProviderTransparent(
          new AssociatedMetadataTypeTypeDescriptionProvider(
              typeof(Stuff),
              typeof(StuffMetadata)), 
          typeof(Stuff)); 
    

    此帮助类将注册程序集中的所有元数据关系:

    public static class MetadataTypesRegister
    {
        static bool installed = false;
        static object installedLock = new object();
    
        public static void InstallForThisAssembly()
        {
            if (installed)
            {
                return;
            }
    
            lock (installedLock)
            {
                if (installed)
                {
                    return;
                }
    
                foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
                {
                    foreach (MetadataTypeAttribute attrib in type.GetCustomAttributes(typeof(MetadataTypeAttribute), true))
                    {
                        TypeDescriptor.AddProviderTransparent(
                            new AssociatedMetadataTypeTypeDescriptionProvider(type, attrib.MetadataClassType), type);
                    }
                }
    
                installed = true;
            }
        }
    }
    

    【讨论】:

    • 为什么 MSDN 上的 MetadataTypeAttribute 列表中没有说明这一点?在我的 MVC 项目中,我已经挂了几个小时 b/c 一切正常,但是在创建我的 Windows 应用程序时,我需要解释一下
    【解决方案2】:

    向 ValidationContext 构造函数提供元数据类的实例而不是主类似乎对我有用。

    【讨论】:

    • 这对我有用 TryValidateProperty 但不幸的是 TryValidateObject
    • @Andy 我只需要 ValidateProperty,所​​以为了缩短代码,我使用了这个 hack 而不是公认答案中的完整解决方案。
    • 我得到一个ArgumentException 那个the instance provided mush match the ObjectInstance on the validationcontext supplied
    • @SlimShaggy 你有一个如何按照你的建议传递元数据类的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2021-11-24
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多