"Move to Resources" Action in Attribute similar to DisplayAttribute


my questino is about the "Move to Resource" action that can be used  when working with the System.ComponentModel.DataAnnotations.DisplayAttribute.
The Attribute has a Name and a ResourceType Property and when you only set the Name property R# provides an action to move this string to resources.
After that it writes in the Name property the resource key and in the resource type the type of the reousrce file.

We now have an attribute which has similar propertys and I would like to have the same behavior.
As I guess there is not built in support for this currently could I write a plugin to do so?  What do I have to look for / use as inspiration?

Thanks for your help.

Kind Regards

Comment actions Permalink

Hi Sven. Given that it's a custom attribute, and somewhat custom behaviour, you will have to write a plugin to do this. To get an idea how to do this, you'll want to fire up dotPeek, point it at the ReSharper directory, and take a look at these classes:

  • DisplayNameAttributePropertyNameReferenceFactory - this class (and the nested Factory class) checks for the DisplayAttribute property and generates a reference (that is, an instance of IReference, more specifically, PublicPropertyReference). Adding a reference like this allows ReSharper to validate the attribute contains a property name that exists on the type. It provides ctrl-click navigation, find usages and renaming support, and will highlight the string as an error if the property name is wrong.
  • DisplayNameAttributeToResourceExtractor is an instance of IResourceExtractor, which (as I understand it) is used by the localisation infrastructure to find elements that can be localised, and to handle what happens after the resource is extracted (changing the parameters to the attribute to include the type of the resource class, and the string key

You'll notice that there are two instances of these classes - one for C# and one for VB. That's because the elements of the parse trees for each file are not compatible with each other (C# and VB are close cousins, but they are different languages, and there are differences in the parse trees). For this instance, you'll probably find the code to be pretty much the same across the two languages.

Hope this helps

Comment actions Permalink

Thanks Matt. I will take a look into this


Please sign in to leave a comment.