gpt4 book ai didi

c# - 自动列出所有方法参数以将它们向前传递

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:12 24 4
gpt4 key购买 nike

如代码所示,传递参数是一项相当频繁的任务。有没有一种方法可以使用 Resharper、Visual Studio native 或其他附加组件自动生成列表 isPriority, label, id, start, user 而不是手动编写?

    public void TransformAndStore(
bool isPriority,
string label,
string id,
DateTimeOffset start,
string user)
{
if (this.IsValid(id)) {
label = this.Clean(label);
this.Reposit(isPriority, label, id, start, user);
}
}

生成的列表应该基于方法签名,按照出现的顺序列出方法的所有参数(不包括类型)。

  • 此外,如果 Reposit 方法要使用调用方法范围内可用的一些但不是全部相同参数(由参数/变量名称标识),那么自动完成也将加快编码速度并减少 RSI :)

最佳答案

您可以创建一个类来存储所有参数,这样您只需传递一个项目。 Resharper 可以通过使用 Refactor -> Extract -> Extract class from parameters 自动为您执行此操作。

此外,如果您总是重复相同的参数组,请考虑将作用于它们的方法也移到类中。使用方法对数据进行逻辑分组可能是一个很好的做法。例如:

public class CleverName
{
public bool IsPriority { get; set; }
public string Label { get; set; }
public string Id { get; set; }
public DateTimeOffset Start { get; set; }
public string User { get; set; }

public bool IsValid()
{
//Check if Id is valid
}

public void TransformAndStore()
{
if (this.IsValid()) {
Label = this.Clean(Label);
this.Reposit();
}
}

public void Reposit()
{
}
}

关于c# - 自动列出所有方法参数以将它们向前传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com