未使用MetadataType加载元数据

本文关键字:元数据 加载 MetadataType 未使用 | 更新日期: 2023-09-27 18:24:56

我有一个关于MetadataType的问题。我有一个DLL助手项目,用于使用LinqToSQL从MS SQL Server访问数据。我还需要为生成的类ClientInfoView添加一个元数据。我已经按照以下方式完成了:

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没有任何属性。

你能帮我找出一个错误吗?

未使用MetadataType加载元数据

因为metadatatype不适用于此类情况,但您可以使用此方法

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

你可以像这样使用

bool res = PropertyHasAttribute<ClientInfoView>("Login", typeof(DisplayAttribute))

这告诉你类属性登录有或没有displayattribute,但如果你需要找到属性Value,你可以使用attribute.GetCustomAttribute方法,并将其强制转换为你选择的属性,如display attribute和读取Name property by。Name:)

要给出部分答案,可以检查ClientInfoView是否具有属性。一些对我有用的小演示。仍在努力寻找为什么我无法访问ClientInfoViewMetaData个人属性中的这些属性

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

或者您可以使用基于elia07答案的扩展方法:

<System.Runtime.CompilerServices.Extension>
Public Function HasAttribute(Of TABLEENTITY, ATTRTYPE)(md As ModelMetadata) As Boolean
    Dim properyName As String = md.ContainerType.GetProperty(md.PropertyName).ToString()
    Dim att As MetadataTypeAttribute = DirectCast(Attribute.GetCustomAttribute(GetType(TABLEENTITY), GetType(MetadataTypeAttribute)), MetadataTypeAttribute)
    If att IsNot Nothing Then
        For Each prop In Type.[GetType](att.MetadataClassType.UnderlyingSystemType.FullName).GetProperties()
            If properyName.ToLower() = prop.Name.ToLower() AndAlso Attribute.IsDefined(prop, GetType(ATTRTYPE)) Then
                Return True
            End If
        Next
    End If
    Return False
End Function

示例:

 Dim md As ModelMetadata = ...
 Dim isReadOnly As Boolean = md.HasAttribute(Of Cikkek, ReadOnlyFW)