[Question] Why setters from properties in attributes are allowed? #3931
-
Suppose that we have an attribute named public sealed class CustomAttribute : Attribute
{
public int Property { get; set; }
} In this case, the property [Custom(Property = 20)]
public sealed class Test
{
// ...
} So... When I use reflection to modify the value
Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The setter needs to exist for the runtime to set the property. That setter only changes that instance, it doesn't rewrite the metadata in the assembly, which is why the value resets when you get the attribute through reflection. If you don't want to have mutable properties in your attributes you can use a constructor instead. |
Beta Was this translation helpful? Give feedback.
The setter needs to exist for the runtime to set the property. That setter only changes that instance, it doesn't rewrite the metadata in the assembly, which is why the value resets when you get the attribute through reflection. If you don't want to have mutable properties in your attributes you can use a constructor instead.