元数据未使用MetadataType加载

我有一些关于MetadataType的问题/问题。 我有使用LinqToSQL从MS SQL Server的数据访问DLL辅助项目。 我还需要为生成的类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没有任何属性。

你能帮我find一个错误吗?

为了给出答案的一部分,你可以检查ClientInfoView已经得到的属性。 一些小的演示为我工作。 仍然试图find为什么我不能访问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; } } 

因为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() == properyName.ToLower() && Attribute.IsDefined(prop,attributeType)) return true; } } return false; } 

你可以像这样使用

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

这告诉你,类属性login具有或没有displayattribute,但是如果你需要find属性值,你可以使用Attribute.GetCustomAttribute方法,并将其转换为您select的属性,如显示属性和阅读名称属性由。

或者你可以使用这个基于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() = properyName.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)