如何提取 ReSharper 模式中字符串的内容?

发布于 2024-10-26 07:39:21 字数 614 浏览 1 评论 0原文

我遇到以下问题:

LINQ 成员表达式替换包含属性名称的硬编码字符串

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

我想用带有 ReSharper 模式的

。以下尝试不起作用:

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

替换模式始终相同:

NotifyOfPropertyChange(() => $prop$);

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-11-02 07:39:21

我认为您不需要 R# 的结构搜索和替换(这很幸运,因为我认为在当前版本中它不能做到这一点)。 Visual Studio 的查找和替换正则表达式应该足够好:

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

( ) 被转义,这样它们就不会成为分组结构; { } 标记与其中的模式匹配,以使它们可用于替换表达式。

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

这里的所有内容都是文字,除了 \1 之外,这意味着“第一个标记表达式”。

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'.

眼眸 2024-11-02 07:39:21

如果我正确理解你的意图,那么这可能会有所帮助。
前段时间我发现了这段代码(不记得最初是谁发布的)

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

所以以下:

NotifyPropertyChange( () => MyProperty)

应该给出结果:

NotifyPropertyChange("MyProperty")

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文