Gerrie Schenck
Gerrie Schenck

Reputation: 22378

What's the use of attributes on an interface?

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?

Upvotes: 4

Views: 3081

Answers (3)

Boris Modylevsky
Boris Modylevsky

Reputation: 3099

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

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064134

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

Upvotes: 8

Robert MacLean
Robert MacLean

Reputation: 39290

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.

Upvotes: 0

Related Questions