orc*_*cun 2 c# resharper refactoring
我一般不太喜欢重构工具.无需深入细节.不过,我偶尔也会试用新版本.以下是我在评估resharper 4.5时尝试做的事情:
我需要用包装器方法(要创建)替换方法的所有用法,但我不能.我常常注意到一个明显的特征,是这种情况吗?如果resharper没有此功能,您知道这些工具吗?
编辑2:示例已得到改进,包括实例方法调用.编辑:这是一个简单的案例.
static void Main(string[] args)
{
while(true)
{
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
Thread.Sleep(10);
if (Quiting()) break;
}
Console.Beep(250, 50);
}
}
static bool Quiting()
{
if (Console.In.Peek() > 0)
{
Console.Beep(250, 150);
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我需要的是:(Edit2:添加了一个实例样本)
private static StringBuilder _builder = new StringBuilder();
static void Main(string[] args)
{
while(true)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape)
{
Thread.Sleep(10);
if (Quiting()) break;
}
_builder.Append(" (").Append(key.KeyChar).Append(") ");
Beep(250, 50);
}
}
static bool Quiting()
{
if (Console.In.Peek() > 0)
{
Beep(250, 150);
_builder.Append('@');
return false;
}
return true;
}
static void Beep(int frequency, int duration)
{
// finally cursor ends up here
Console.Beep(250, 50);
}
Run Code Online (Sandbox Code Playgroud)
Console.Beep调用被重构.接下来让重构StringBuilder.Append(char):
class Program
{
private static StringBuilder _builder = new StringBuilder();
static void Main(string[] args)
{
while(true)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape)
{
Thread.Sleep(10);
if (Quiting()) break;
}
_builder.Append(" (").AppendUpper(key.KeyChar).Append(") ");
Beep(250, 50);
}
}
static bool Quiting()
{
if (Console.In.Peek() > 0)
{
Beep(250, 150);
_builder.AppendUpper('n');
return false;
}
return true;
}
static void Beep(int frequency, int duration)
{
// finally cursor ends up here
Console.Beep(250, 50);
}
}
static class StringBuilderExtensions
{
public static StringBuilder AppendUpper(this StringBuilder builder, char c)
{
return builder.Append(char.ToUpper(c));
}
}
Run Code Online (Sandbox Code Playgroud)
从用法中选择并且可能省略公共参数(例如上面的250)或非扩展静力学的常见实例参数将使该特征更有价值.希望这能解决问题.
ReSharper没有将其作为单个重构.我可以这样做:
编辑: 根据您的编辑,似乎您还有一个问题.Console.Beep不仅不在同一个班级,甚至不在你的班级.
但是如果你不介意一点点搜索和替换,那么你可以把它放到你自己的类中,然后继续重构:
namespace Temporary {
public class Console {
public static void Beep(int x, int y) {System.Console.Beep(x,y);}
}
}
Run Code Online (Sandbox Code Playgroud)
然后执行"替换文件"以使用Temporary.Console.Beep替换Console.Beep,并按上述步骤操作.
| 归档时间: |
|
| 查看次数: |
1363 次 |
| 最近记录: |