内联参数重构
此重构允许您用方法调用中的参数值替换方法参数。 如果有多个调用,您可以选择从哪个调用中获取参数。
一个简单的案例。 内联参数 pi 的常量值:
private double AreaOfCircle(double rad, double pi)
{
return pi*rad*rad;
}
public void Test()
{
var area = AreaOfCircle(10, Math.PI);
}
private double AreaOfCircle(double rad)
{
return Math.PI*rad*rad;
}
public void Test()
{
var area = AreaOfCircle(10);
}
如果您想要内联的参数依赖于其他变量和/或计算,JetBrains Rider 可以用其他参数替换原始参数,并将计算移到目标方法内部。
In the following example, we apply the refactoring to the 操作 parameter of the 执行操作 method so that the whole lambda, which was used as an argument in the call moves into the method body, and two new parameters are created to pass necessary values:
private void PerformAction(Action action)
{
action();
}
private void Test(string key, string value)
{
PerformAction(() => Console.WriteLine("{0} : {1}", key, value));
}
private void PerformAction(string arg0, string value)
{
((Action) (() => Console.WriteLine("{0} : {1}", arg0, value)))();
}
private void Test(string key, string value)
{
PerformAction(key, value);
}
内联一个参数
将光标放在方法声明中的参数或方法调用中的参数上。
请执行以下操作之一:
按 Ctrl+Alt+N 然后选择 内联参数
按 Ctrl+Alt+Shift+T 然后选择 内联参数。
在主菜单中选择 。
内联参数 对话框将打开。
如果方法有多个用法,请选择您想要内联参数的用法,然后单击 下一步。
选择您想要内联的参数。
如果内联的参数依赖于其他变量,JetBrains Rider 会在 新参数 字段中建议一个或多个来自调用者的变量。 选择所需的参数。
检查新签名的预览并单击 下一步。
如果未发现冲突,JetBrains Rider会立即执行重构。 否则,它会提示您 解决冲突。
最后修改日期: 2025年 9月 26日