Get member to which attribute was applied from inside attribute constructor?

后端 未结 3 1450
遇见更好的自我
遇见更好的自我 2020-11-30 10:11

I have a custom attribute, inside the constructor of my custom attribute I want to set the value of a property of my attribute to the type of the property my attribute was a

3条回答
  •  眼角桃花
    2020-11-30 10:48

    Attributes don't work that way, I'm afraid. They are merely "markers", attached to objects, but unable to interact with them.

    Attributes themselves should usually be devoid of behaviour, simply containing meta-data for the type they are attached to. Any behaviour associated with an attribute should be provided by another class which looks for the presence of the attribute and performs a task.

    If you are interested in the type the attribute is applied to, that information will be available at the same time you are reflecting to obtain the attribute.

提交回复
热议问题