检索属性的名称和值具有特定的属性值

假设有一个属性,例如:

public class ValueAttribute : Attribute
{
  public int Val;
  public ValueAttribute(int val)
  {
    Val = val;
  }
}

它适用于一个类:

public class Person
{
  [Value(10)]
  public string FirstName;
  [Value(20)]
  public string LastName;
  [Value(30)]
  public string Age;
}

How would I go about efficiently retrieving a PropertyInfo (preferably without loop/iteration) of the first occurrence of a property that has Value(20)?

评论