Metadata were not loaded using MetadataType(未使用 MetadataType 加载元数据)
问题描述
我有一些关于 元数据类型.我有使用 LinqToSQL 从 MS SQL Server 访问数据的 DLL 帮助程序项目.我还需要为生成的类 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.
你能帮我找出错误吗?
推荐答案
要给出部分答案,您可以检查 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 加载元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未使用 MetadataType 加载元数据


- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01