使用MetadataType没有加载元 [英] Metadata were not loaded using MetadataType

查看:141
本文介绍了使用MetadataType没有加载元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题/问题关于<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.metadatatypeattribute.aspx"相对=nofollow> MetadataType 。我有DLL辅助项目从MS SQL Server中使用LinqToSQL数据访问。我还需要添加元数据生成的类ClientInfoView。我已经做到了以下方式:

I've got a some problem/question about MetadataType. I've got DLL helper-project for data access from MS SQL Server using LinqToSQL. I've also need to add a metadata for a generated class ClientInfoView. I've done it following way:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace DataAPI.LINQToSQL
{
    [MetadataType(typeof(ClientInfoViewMetaData))]
    public partial class ClientInfoView
    {
        internal sealed class ClientInfoViewMetaData
        {
            [Category("Main Data"), DisplayName("Client ID")]
            public int ID { get; set; }

            [Category("Main Data"), DisplayName("Login")]
            public string Login { get; set; }

            ...
        }
    }
}

但是,当我检查了运行时的属性,我发现ClientInfoView没有得到任何属性。

But when I checked the attributes in runtime, I've found that ClientInfoView hasn't got any attributes.

能否请你帮我找错了吗?

Can you please help me to find a mistake?

推荐答案

要给出答案的某些部分,你可以检查ClientInfoView已得到属性。一些小演示,为我工作。仍在尝试查找为什么我不能访问ClientInfoViewMetaData各个属性的属性。

To give some part of the answer , you can check ClientInfoView has got attributes. Some small demo that worked for me. Still trying to find why I can't access those attributes in ClientInfoViewMetaData individual properties

    static void Main(string[] args)
    {
        TypeDescriptor.AddProviderTransparent(
        new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ClientInfoView), typeof(ClientInfoViewMetaData)), typeof(ClientInfoView));
        ClientInfoView cv1 = new ClientInfoView() { ID = 1 };
        var df = cv1.GetType().GetCustomAttributes(true);
        var dfd = cv1.ID.GetType().GetCustomAttributes(typeof(DisplayNameAttribute), true);
        var context = new ValidationContext(cv1, null, null);
        var results = new List<ValidationResult>();
        var isValid = Validator.TryValidateObject( cv1,context, results, true);
    }
}

    [MetadataType(typeof(ClientInfoViewMetaData))]
    public partial class ClientInfoView
    {
        public int ID { get; set; }
        public string Login { get; set; }
    }

public class ClientInfoViewMetaData
{        
    [Required]
    [Category("Main Data"), DisplayName("Client ID")]
    public int ID { get; set; }

    [Required]
    [Category("Main Data"), DisplayName("Login")]
    public string Login { get; set; }

}

这篇关于使用MetadataType没有加载元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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