Issue with Attribute.IsDefined

Hi,
I am trying to check if a variable has a TextArea attribute, but even if it does it still returns false.
What am I doing wrong here?

public class Product
{
    [SerializeField] string ID;
    [SerializeField] string Name;
    [TextArea(4, 8)]
    [SerializeField] string Description;
    [SerializeField] double price;
}

Product product;

public void GetProperties()
{
    PropertyInfo[] properties = product.GetType().GetProperties();
    foreach (PropertyInfo property in properties)
    {
        Debug.Log(Attribute.IsDefined(property, typeof(TextAreaAttribute)));
    }
}

I agree it does not seem to work, at least in Editor. Have you tried making a build?

Based on the API it seems it should perhaps say true, but the example given by Microsoft docs gives ParameterArrayAttribute as an example, and then they apply it by using the keyword in the args declaration, rather than as a pure decorator, sort of muddying the water as compared with yours. Perhaps it’s identical, I’m not enough of a C# weenie to know.

oh duh, it’s a field, not a property. Use GetField and FieldInfo.

        FieldInfo[] fields = product.GetType().GetFields();
        foreach (FieldInfo field in fields)
        {
            var ggg = field.Name;
            Debug.Log(ggg + " - " + Attribute.IsDefined(field, typeof(TextAreaAttribute)));
        }

6376635--710019--Screen Shot 2020-10-02 at 12.19.27 PM.png

1 Like

Yup I just figured it out as well :smile: Silly mistakes… Thanks anyways!

2 Likes