首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元数据未使用MetadataType加载

元数据未使用MetadataType加载
EN

Stack Overflow用户
提问于 2013-07-26 05:26:57
回答 2查看 2.3K关注 0票数 0

关于MetadataType,我有一个问题。我有DLL帮助项目,用于使用LinqToSQL从访问数据。我还需要为生成的类ClientInfoView添加元数据。我是这样做的:

代码语言:javascript
复制
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没有任何属性。

你能帮我找个错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-26 06:50:28

为了给出部分答案,您可以检查ClientInfoView是否有属性。一些对我有用的小演示。仍然试图找出为什么我不能访问ClientInfoViewMetaData单独属性中的那些属性

代码语言:javascript
复制
    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; }

}
票数 1
EN

Stack Overflow用户

发布于 2014-07-15 11:51:02

因为元数据类型并不适用于这类组件,但是您可以使用此方法。

代码语言:javascript
复制
private bool PropertyHasAttribute<T>(string properyName, Type attributeType)
    {
        MetadataTypeAttribute att = (MetadataTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(MetadataTypeAttribute));
        if (att != null)
        {
            ;
            foreach (var prop in Type.GetType(att.MetadataClassType.UnderlyingSystemType.FullName).GetProperties())
            {
                if (properyName.ToLower() == prop.Name.ToLower() && Attribute.IsDefined(prop,attributeType))
                    return true;
            }
        }
        return false;
    }

你可以这样用

代码语言:javascript
复制
bool res = PropertyHasAttribute<ClientInfoView>("Login", typeof(DisplayAttribute))

这告诉您类属性登录有或没有显示属性,但是如果需要查找属性值,可以使用Attribute.GetCustomAttribute方法将其转换为所选属性,如.Name :)显示属性和读取名称属性。)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17873576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档