Resharper 或 CodeRush - 将标志从负更改为正

发布于 2024-08-05 23:06:47 字数 439 浏览 2 评论 0 原文

我有一个带有否定标志的类(“DoNot{Action}”)。我想将此标志更改为肯定的“Do”并更改代码中的所有检查,基本上否定它们。有这样的重构吗?

这是一个例子。如果我将标志 DoNotFilter 更改为 DoFilter,则查看此标志的所有代码都应相应更改。

public bool DoNotFilter
{get;private set;}

更改为

public bool DoFilter
{get;private set;}

所以

if (attribute.DoNotFilter == true)

需要更改为

if (attribute.DoFilter == false)

I have a class that has negative flag ("DoNot{Action}"). I would like to change this flag to positive "Do" and change all checks in the code, basically negating them. Is there such refactoring?

Here is an example. If I change the flage DoNotFilter to DoFilter all code that looks at this flag should change accordingly.

public bool DoNotFilter
{get;private set;}

changes to

public bool DoFilter
{get;private set;}

so

if (attribute.DoNotFilter == true)

need to change to

if (attribute.DoFilter == false)

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

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

发布评论

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

评论(2

夏末染殇 2024-08-12 23:06:47

您也许能够在 ReSharper 中进行内联属性重构来执行您想要的操作。不过,我不确定它是否适用于属性。

示例:

public bool DoNotFilter
{ get; private set; }

public void Foo()
{
    if (DoNotFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

现在更改为:

public bool DoFilter
{ get; private set; }

public bool DoNotFilter
{ get { return !DoFilter; } }

现在重构 DoNotFilter 和内联属性:

public void Foo()
{
    if (!DoFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

如果您尝试这样做,请务必首先将所有内容签入源代码管理! :) 没有保证!

您还可以删除多余的比较,将 if 简化为:

if (!DoFilter)

You might be able to get the Inline Property refactoring in ReSharper to do what you want. I'm not sure if it will work with attributes, though.

Example:

public bool DoNotFilter
{ get; private set; }

public void Foo()
{
    if (DoNotFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

Now change to:

public bool DoFilter
{ get; private set; }

public bool DoNotFilter
{ get { return !DoFilter; } }

Now refactor DoNotFilter and Inline Property:

public void Foo()
{
    if (!DoFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

If you try this, be sure to check everything in to source control first! :) No warranties!

You can also Remove redundant comparision to get the if down to:

if (!DoFilter)
夏末的微笑 2024-08-12 23:06:47

您可以使用 Visual Studio 查找和替换对话框。要启动该对话框,请按 Ctrl+H。

确保选中“使用”复选框,并在下拉列表中选择“正则表达式”。我还会选中“搜索隐藏文本”和“搜索”复选框。

在“查找内容”中输入:

在“替换为”中输入:Do\1

它将删除所有出现的 DoNotXXX 中的“Not”。

接下来让替换

if (attribute.DoFilter == true)

if (attribute.DoFilter == false)

如果您关闭了“查找和替换”对话框,请再次按 Ctrl+H。

在“查找内容”中输入:

在“替换为”中输入:Do\1 == false

You can use Visual Studio Find and Replace dialog. To lunch the dialog press Ctrl+H.

Make sure that "Use" check box is checked and in dropdown list "Regular expression" is selected. I also would check "Search hidden text" and "Search up" check boxes.

In "Find what" type: <DoNot{:i+}

In "Replace with" type: Do\1

It will remove "Not" In all occurrences of DoNotXXX.

Next let replace

if (attribute.DoFilter == true)

with

if (attribute.DoFilter == false)

If you closed Find and Replace dialog, press Ctrl+H again.

In "Find what" type: <Do{:i+} == true

In "Replace with" type: Do\1 == false

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