Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper Custom Patterns: Ignore property attributes

I am writing some Resarper Custom Patterns to warn us about some code constructs that need attention. One of these is replacing OnpropertyChanged("String") with a lambda variant OnPropertyChanged(() => propertyname)

The search Pattern I defined is:

 public $type$ $property$
 {
            get { return $backingfield$; }
            set
            {
                if($backingfield$  != value) {
                    $backingfield$ = value;
                    OnPropertyChanged($String$);
                }
           }
 }

This pattern is being replaced with:

public $type$ $property$
{
        get { return $backingfield$; }
        set
        {
            if($backingfield$  != value) {
                $backingfield$ = value;
                OnPropertyChanged(() => $property$);
            }
        }
}

Problem: When applying this, Resharper throws away the attributes defined on the property. This snippet:

[MyAttribute]
public int Test
{
            get { return _Test; }
            set
            {
                if (_Test != value)
                {
                    _Test = value;
                    OnPropertyChanged("Test");
                }
            }
}

gets replaced with

public int Test
{
            get { return _Test; }
            set
            {
                if (_Test != value)
                {
                    _Test = value;
                    OnPropertyChanged(() => Test);
                }
            }
}

How can I preserve the attributes??

UPDATE: Adding a type placeholder derived from System.Attribute to both search and replace pattern fixes it partially.

[$Attributes$]
...

Remaining problem is that the Attribute placeholder only matches one attribute, it fails on multiple attributes.

like image 320
Yoeri Avatar asked Nov 05 '22 05:11

Yoeri


1 Answers

If you cannot get another solution there is a workaround.
You use your Search pattern (without using replace pattern) to show the warnings. I think that works already.
Then you create a Surround Template that replaces a string to ()=>PropName. See the picture for an example:

enter image description here

Then you have the warnings by Search pattern and the replacing by a Surround Template.
The usage is: If you see the warning select the string, press Ctrl+E, Ctrl+U and select template String to func returning property.

Of course the string selection is bothering. But that is the best that I have found out up to now.

like image 109
brgerner Avatar answered Nov 12 '22 17:11

brgerner