답변:
속성을 검색하는 빠른 방법은 없습니다. 그러나 코드는 다음과 같아야합니다 ( Aaronaught의 신용 ) :
var t = typeof(YourClass);
var pi = t.GetProperty("Id");
var hasIsIdentity = Attribute.IsDefined(pi, typeof(IsIdentity));
속성 속성을 검색해야하는 경우
var t = typeof(YourClass);
var pi = t.GetProperty("Id");
var attr = (IsIdentity[])pi.GetCustomAttributes(typeof(IsIdentity), false);
if (attr.Length > 0) {
// Use attr[0], you'll need foreach on attr if MultiUse is true
}
[NotMapped]는 클래스에서와 같이 사용 되지만이를 감지하려면 다음을 사용해야합니다.Attribute.IsDefined(pi, typeof(NotMappedAttribute))
IsIdentity[] attr = pi.GetCustomAttributes<IsIdentity>(false);
.NET 3.5를 사용하는 경우 식 트리를 사용해 볼 수 있습니다. 반사보다 안전합니다.
class CustomAttribute : Attribute { }
class Program
{
[Custom]
public int Id { get; set; }
static void Main()
{
Expression<Func<Program, int>> expression = p => p.Id;
var memberExpression = (MemberExpression)expression.Body;
bool hasCustomAttribute = memberExpression
.Member
.GetCustomAttributes(typeof(CustomAttribute), false).Length > 0;
}
}
공통 (일반) 방법을 사용하여 지정된 MemberInfo에서 속성을 읽을 수 있습니다.
public static bool TryGetAttribute<T>(MemberInfo memberInfo, out T customAttribute) where T: Attribute {
var attributes = memberInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
if (attributes == null) {
customAttribute = null;
return false;
}
customAttribute = (T)attributes;
return true;
}
@Hans Passant의 답변을 업데이트 및 / 또는 향상시키기 위해 부동산 검색을 확장 방법으로 분리합니다. GetProperty () 메소드에서 불쾌한 마술 문자열을 제거 할 수 있다는 추가 이점이 있습니다.
public static class PropertyHelper<T>
{
public static PropertyInfo GetProperty<TValue>(
Expression<Func<T, TValue>> selector)
{
Expression body = selector;
if (body is LambdaExpression)
{
body = ((LambdaExpression)body).Body;
}
switch (body.NodeType)
{
case ExpressionType.MemberAccess:
return (PropertyInfo)((MemberExpression)body).Member;
default:
throw new InvalidOperationException();
}
}
}
그런 다음 테스트는 두 줄로 줄어 듭니다.
var property = PropertyHelper<MyClass>.GetProperty(x => x.MyProperty);
Attribute.IsDefined(property, typeof(MyPropertyAttribute));
휴대용 클래스 라이브러리 PCL (예 : 나와 같은) 에서이 작업을 수행하려는 경우 다음과 같이하십시오.
public class Foo
{
public string A {get;set;}
[Special]
public string B {get;set;}
}
var type = typeof(Foo);
var specialProperties = type.GetRuntimeProperties()
.Where(pi => pi.PropertyType == typeof (string)
&& pi.GetCustomAttributes<Special>(true).Any());
그런 다음 필요한 경우이 특수 속성이있는 속성 수를 확인할 수 있습니다.
Attribute.IsDefined 메소드를 사용할 수 있습니다
https://msdn.microsoft.com/en-us/library/system.attribute.isdefined(v=vs.110).aspx
if(Attribute.IsDefined(YourProperty,typeof(YourAttribute)))
{
//Conditional execution...
}
구체적으로 찾고있는 속성을 제공하거나 다음과 같이 리플렉션을 사용하여 모든 속성을 반복 할 수 있습니다.
PropertyInfo[] props = typeof(YourClass).GetProperties();
이것은 꽤 오래된 질문이지만 사용했습니다.
내 방법에는이 매개 변수가 있지만 빌드 할 수 있습니다.
Expression<Func<TModel, TValue>> expression
그런 다음이 방법에서 :
System.Linq.Expressions.MemberExpression memberExpression
= expression.Body as System.Linq.Expressions.MemberExpression;
Boolean hasIdentityAttr = System.Attribute
.IsDefined(memberExpression.Member, typeof(IsIdentity));
Attribute.IsDefined코드를 사용 하면 한 줄의 코드와 못생긴 배열 / 캐스팅이 제거됩니다.