Se si applicano attributi a una classe parziale tramite MetadataType attribute, tali attributi non vengono trovati tramite Attribute.IsDefined(). Qualcuno sa perché, o cosa sto facendo male?Attribute.IsDefined non visualizza gli attributi applicati con la classe MetadataType
Di seguito è riportato un progetto di test che ho creato per questo, ma sto davvero cercando di applicare gli attributi personalizzati a una classe di entità LINQ to SQL, ad esempio this answer in this question.
Grazie!
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
{
}
}
controllo questo questo fuori, ho già risposto a questa domanda qui http://stackoverflow.com/a/24757520/3050647 – elia07
controllo questo questo, ho già risposto a questa domanda qui http://stackoverflow.com/a/24757520/3050647 – elia07