Si je demande des attributs à une classe partielle par le MetadataType attribute, ces attributs ne sont pas trouvés par Attribute.IsDefined(). Quelqu'un sait pourquoi, ou ce que je fais mal? Voici un projet de test que j'ai créé pour cela, mais j'essaie vraiment d'appliquer des attributs personnalisés à une classe d'entités LINQ to SQL - comme this answer in this question.Attribute.IsDefined ne voit pas les attributs appliqués avec classe MetadataType
Merci!
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace MetaDataTest
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] properties = typeof(MyTestClass).GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
// Displays:
// False
// False
// 0
}
Console.ReadLine();
}
}
[MetadataType(typeof(MyMeta))]
public partial class MyTestClass
{
public string MyField { get; set; }
}
public class MyMeta
{
[MyAttribute()]
public string MyField { get; set; }
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : System.Attribute
{
}
}
vérifier ce ceci, je l'ai déjà répondu à cette question ici http://stackoverflow.com/a/24757520/3050647 – elia07
vérifier cette ceci dehors, j'ai déjà répondu à cette question ici http://stackoverflow.com/a/24757520/3050647 – elia07