What's the use of attributes on an interface?

有些话、适合烂在心里 提交于 2019-12-23 12:38:42

问题


Using Resharper, I extracted an interface of an existing class. This class has some attributes set on a few members, and Resharper also put these on the interface members.

Can I delete these attributes from the interface? Attributes on an interface aren't inherited when implementing an interface right?


回答1:


They aren't used by the implementing class - but still might be critical. For example, WCF defines service-contracts and operation-contracts by the attributes on the interface.

If they aren't of use in your case, then delete them. Otherwise, leave them alone ;-p




回答2:


They aren't inherited but depending on the attribute you can still do interesting things. For instance if you are using the validation block from the entlib, you can use the attributes on the interface to do validation on all classes derived from it without needing to retype the validation rules on the classes.




回答3:


I agree that this is quite annoying when class attributes are automatically copied to the extracted interface. There is an open issue on TeamCity youTrack. The interesting thing is that it was open 7 (seven!!!) years ago and has only 3 votes (including myself).

Upvote if you agree:
Extract interface moves class property attributes to the interface



来源:https://stackoverflow.com/questions/590336/whats-the-use-of-attributes-on-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!