使用验证时,.NET 4 RTM MetadataType属性被忽略 [英] .NET 4 RTM MetadataType attribute ignored when using Validator

查看:645
本文介绍了使用验证时,.NET 4 RTM MetadataType属性被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS 2010 RTM,并试图使用MetadataTypeAttribute一个简单的类型执行一些基本的验证。当我把验证属性上的主类,一切正常。然而,当我把它的元数据类,它似乎被忽略。我必须失去一些小事,但我一直停留在这一段时间吧。

我看了一下企业库验证模块作为一种解决方法,但它不支持单属性的确认开箱。任何想法?

 类节目
{
    静态无效的主要(字串[] args)
    {
        东西T =新的东西();

        尝试
        {
            Validator.ValidateProperty(德克萨斯州,新ValidationContext(T,NULL,NULL){成员名=X});
            Console.WriteLine(失败!);
        }
        赶上(ValidationException)
        {
            Console.WriteLine(成功了!);
        }
    }
}

[MetadataType(typeof运算(StuffMetadata))]
公共类的东西
{
    // [必填] //这里工作
    公共序列X {获得;组; }
}

公共类StuffMetadata
{
    [必填] //这里没有任何影响
    公共序列X {获得;组; }
}
 

解决方案

这似乎验证不尊重MetadataTypeAttribute:

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

的关系必须显式注册的:

  TypeDescriptor.AddProviderTransparent(
      新AssociatedMetadataTypeTypeDescriptionProvider(
          typeof运算(东西),
          typeof运算(StuffMetadata)),
      typeof运算(东西));
 

这个辅助类将注册在装配中的所有元数据关系:

 公共静态类MetadataTypesRegister
{
    安装静态布尔=假;
    静态对象installedLock =新的对象();

    公共静态无效InstallForThisAssembly()
    {
        如果(安装)
        {
            返回;
        }

        锁定(installedLock)
        {
            如果(安装)
            {
                返回;
            }

            的foreach(类型类型Assembly.GetExecutingAssembly()。GetTypes())
            {
                的foreach(MetadataTypeAttribute ATTRIB在type.GetCustomAttributes(typeof运算(MetadataTypeAttribute),真))
                {
                    TypeDescriptor.AddProviderTransparent(
                        新AssociatedMetadataTypeTypeDescriptionProvider(类型,attrib.MetadataClassType),类型);
                }
            }

            安装= TRUE;
        }
    }
}
 

I am using VS 2010 RTM and trying to perform some basic validation on a simple type using MetadataTypeAttribute. When I put the validation attribute on the main class, everything works. However, when I put it on the metadata class, it seems to be ignored. I must be missing something trivial, but I've been stuck on this for a while now.

I had a look at the Enterprise Library validation block as a workaround, but it doesn't support validation of single properties out of the box. Any ideas?

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

解决方案

It seems that the Validator doesn't respect MetadataTypeAttribute:

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

The relationship must be explicity registered:

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

This helper class will register all the metadata relationships in an assembly:

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

这篇关于使用验证时,.NET 4 RTM MetadataType属性被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆