开发者

How do I extract the contents of a string within a ReSharper Pattern?

开发者 https://www.devze.com 2023-02-19 16:35 出处:网络
I\'ve got the following problem: I want to replace a hardcoded string which contains a property name with a LINQ member expression

I've got the following problem:

I want to replace a hardcoded string which contains a property name with a LINQ member expression

// like this:
NotifyOfPropertyChange("MyProperty");
// becomes
NotifyOfPropertyChange(() => MyProperty);

with a ReSharper pattern.

The following attempts didn't work:

NotifyOfPropertyChange("$prop$"); // ($prop$ is of type Identifier, results in parse error)
NotifyOfPropertyChange($prop$); // ($prop$ is of type Expression [System.String],  
                            开发者_如何转开发    // almost works, but without removing the quotes

The replace pattern was always the same:

NotifyOfPropertyChange(() => $prop$);

Any ideas?


I don't think you need R#'s Structural Search and Replace here (which is fortunate, because I don't think in the present version it can do this). Visual Studio's Find and Replace regexes should be good enough:

Find What: 
NotifyPropertyChange\("{.*}"\)

The ( ) are escaped so that they dontg become a grouping construct; the { } tag whatever matches the pattern within to make them available to the replace expression

Replace with:
NotifyPropertyChange(() => \1)

Everything here is literal except \1, which means 'the first tagged expression'.


If I understand your intention correctly then this maybe will help. Some time ago I found this piece of code ( can't remember who posted it originally)

public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
    {
        return (propertyExpression.Body as MemberExpression).Member.Name;
    }

So the following:

NotifyPropertyChange( () => MyProperty)

should give a result:

NotifyPropertyChange("MyProperty")
0

精彩评论

暂无评论...
验证码 换一张
取 消