MetadataType attribute을 통해 부분 클래스에 특성을 적용하면 이러한 특성은 Attribute.IsDefined()을 통해 발견되지 않습니다. 누구나 왜, 또는 내가 뭘 잘못 알고있어?Attribute.IsDefined에 MetadataType 클래스에 적용된 특성이 표시되지 않습니다.
다음은 이것을 위해 만든 테스트 프로젝트이지만 LYQ-SQL 엔터티 클래스에 사용자 지정 특성을 적용하려고합니다 (예 : this answer in this question).
감사합니다.
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
{
}
}
확인이 이것을, 난 이미 여기 http://stackoverflow.com/a/24757520/3050647 – elia07
확인이이 질문에 대답 @AdamGrid 대답을 사용하여, 나는이 같은 코드가 얻을 수정 이 질문에 이미 대답했습니다. http://stackoverflow.com/a/24757520/3050647 – elia07