[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
...
}
I want this custom attribute used both on properties and fileds but not others. How do I assign multiple targets(AttributeTargets.Property and AttributeTargets.Field)? Or It's just not possible?
And AttributeTargets.All is not what I want.
You can specify multiple targets like this, by using the | (bitwise OR) operator to specify multiple enum values:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MyAttribute : Attribute
{
    ...
}
The bitwise OR operator works with the AttributeTargets enum because its values are assigned a particular way and it's marked with the Flags attribute.
If you care to, you can read more here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With