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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With