Check if property has attribute

前端 未结 8 1537
刺人心
刺人心 2020-11-28 03:03

Given a property in a class, with attributes - what is the fastest way to determine if it contains a given attribute? For example:

    [IsNotNullable]
    [I         


        
8条回答
  •  迷失自我
    2020-11-28 03:21

    This is a pretty old question but I used

    My method has this parameter but it could be built:

    Expression> expression
    

    Then in the method this:

    System.Linq.Expressions.MemberExpression memberExpression 
           = expression.Body as System.Linq.Expressions.MemberExpression;
    Boolean hasIdentityAttr = System.Attribute
           .IsDefined(memberExpression.Member, typeof(IsIdentity));
    

提交回复
热议问题