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