natli
natli

Reputation: 3822

Attribute on property not found

[MyAttribute("x")]
public string Z{get;set;}

[MyAttribute("x")]
public void Y()
{

}

It finds the attr on the method just fine, but the one on the property isn't recognized.

public static bool HasAttribute(this MethodInfo m, Type attrType)
{
    return Attribute.IsDefined(m, attrType);
}

I looked at the object during debugging and at the method it correctly lists the Attribute at CustomAttributes, but the one on the property is empty... can anyone explain?

Upvotes: 2

Views: 476

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 100029

You defined the attribute on the property, not on the property accessors. To provide the attribute on the getter, you would use this syntax (I included all 3 possible locations, you can choose to add the attribute on any combination depending on its applicability).

[MyAttribute("on PropertyInfo")]
public string Z
{
    [MyAttribute("on getter MethodInfo")] get;
    [MyAttribute("on setter MethodInfo")] set;
}

Upvotes: 2

Related Questions