How to an method attribute as a property name, like in NPC?

I would like to create some additional annotation attributes, something like

[DataSourceParam] [DataSourceMemberNameParam]

to mark some method parameters as a property names of e.g. type from another parameter. It should work just like NotifyPropertyChange invokator  method param. Better with intellisense and error highlighting. I have found out how to add highlightings for wrong property names. Just copy/paste from CallerInfoAttributesProblemAnalyzer. But but how to tell R#, "this param is a property name"? And to specify a type to get the properties from for intellisense? Even better if the rename refactoring would take such string literals into account. Not too much wishes?

Comment actions Permalink

For you clarity -  the thread should be called like:

"How to create a custom annotation attribute"
"How to mark method string argument as a property name"
   the R# does it somehow for NotifyPropertyChanged handler argument. And I want just extend this functionality to other custom methods annotated with an attribute.

P.S. This discussion forum server is sometimes really sluggish!

Comment actions Permalink

Hi Andreas. ReSharper doesn't offer this functionality out of the box, but it can be implemented pretty easily with a plugin. ReSharper already does a similar thing in the NUnit support - if you use [TestCaseSource] attribute, it provides code-completion for the parameter, giving you a choice of properties defined on the current type, or on the type listed in the attribute constructor. It also creates a reference between the string parameter and the property, marking the property in use and enabling navigation (go to declaration and find usages) from the string reference. It also means the string is renamed when the property is, and the string is marked as an error if it contains a name that doesn't match a property on the class.

You can see how to implement this by opening the ReSharper bin directory in dotPeek, and looking for TestCaseSourceReferenceProviderFactory. This class creates an instance of TestCaseSourceReferenceFactory, which in turn creates references between the string literal and the property. You would need to replicate these two classes, and an equivalent for the TestCaseSourceReference, which represents the reference. Finally, to enable code-completion, you need to implement a class similar to TestCaseSourceReferenceSuggestionRule.

Hope this helps

Comment actions Permalink

Thank you Matt! This was very, VERY helpful.
My intention was already to create a plugin. I have disassembled the R# code with R# directly in Visual Stuidio in my R# plugin project. I have also seen the CSharpPropertyReference and the related Providers+Factories within R# code, but without explanations I had no clue for what it is good.
Now working on suggestion rule...

Comment actions Permalink
Good stuff! You'll have to share a link once you've got it working...

Please sign in to leave a comment.