Jeśli zastosuję atrybuty do klasy częściowej za pomocą MetadataType attribute, atrybuty te nie zostaną znalezione przez Attribute.IsDefined(). Ktoś wie dlaczego, lub co robię źle?Funkcja Attribute.IsDefined nie wyświetla atrybutów zastosowanych w klasie MetadataType
Poniżej znajduje się projekt testowy, który stworzyłem do tego, ale naprawdę próbuję zastosować niestandardowe atrybuty do klasy jednostek LINQ do SQL - jak this answer in this question.
Dzięki!
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
{
}
}
sprawdzenie tego to, ja już odpowiedział na to pytanie tutaj http://stackoverflow.com/a/24757520/3050647 – elia07
to check to, już odpowiedziałem na to pytanie tutaj http://stackoverflow.com/a/24757520/3050647 – elia07