如何将 Tuple<..> 重构为具有描述性名称的类型化 class?
How can I refactor Tuple<..> to a typed class with descriptive names?
上下文
我继承了一个大量使用 Tuple<...> 类型的遗留代码库。代码充满了这样的行:
myVar = myTuple.Item4;
我想通过引入具有描述性 属性 名称的自定义 类 来重构此代码以提高可读性。
问题
是否有任何工具可以有效支持此重构操作?
我认为这个答案不会完全符合您的期望,但既然我去过那里,我想我会分享我的做法。
不幸的是,我没有找到使用某些工具完全自动化此重构的方法。但是,我是这样处理这个问题的...
如果用元组来表示你现在要介绍的单个class,那你就走运了。我想情况并非如此,因此您将不得不进行一些分类。
但首先要做的是...
寻找元组
我查找了所有使用Tuple
的地方。
独立 Visual Studio 和 Resharper 都可以通过使用 Find Usages 或 Find Usages Advanced(如果使用 Tuple.Create()
).
创建元组
否则或另外,您将需要搜索 new Tuple<
或 Tuple
.
的解决方案
根据这些信息,确定需要介绍的第一个 class。
1。创建 class
... 现在让它看起来像 Tuple
。 XML-Doc 可以在稍后的重构中处理 class 时提供帮助。
class ClassThatWasMissing
{
/// <summary>
/// Represents some Property.
/// </summary>
int Item1 { get; set; }
/// <summary>
/// Represents some other Property.
/// </summary>
string Item2 { get; set; }
}
2。用 ClassThatWasMissing
的声明和初始化替换 Tuple
-creation
- Tuple<int, string> myvar = new Tuple<int, string>();
+ ClassThatWasMissing myvar = new ClassThatWasMissing();
myvar.Item1 = 42;
myvar.Item2 = "Some information";
3。重构 ClassThatWasMissing
为属性 (Item{n}
) 提供更具描述性的名称。上面的评论此时就可以派上用场了。
同样,Resharper 和独立 Visual Studio 都可以提供帮助(例如 R#:重构 → 重命名):
class ClassThatWasMissing
{
/// <summary>
/// Represents some Property.
/// </summary>
int SomeProperty { get; set; }
/// <summary>
/// Represents some other Property.
/// </summary>
string SomeOtherProperty { get; set; }
}
在这一点上还要记住,有时在使用点而不是在声明点应用名称可能更容易,因为它们的目的在这些点可能更明显。 VS 和 R# 都可以从那里重命名属性。
ClassThatWasMissing myvar = new ClassThatWasMissing();
myvar.SomeProperty = 42;
myvar.SomeOtherProperty = "Some information";
重复该过程
...对于您最初或连续找到的所有地点。这样做直到没有 Tuple
剩余(或至少只有您可以忍受的)。
祝你好运!
(2021年访题)
如果可用,您可以重构为 c# 7+ Tuple types。
我认为这个过程有两个阶段:
- 正则表达式从
Tuple<Type1, Type2 ...>
到 (Type1 Item1, Tupe2 Item2 ...)
的声明
- 重命名(例如使用 VS 的
Crtl+R
、Ctrl+R
)从 Item1
到 AmazingName
、Item2
到 EvenBetterName
、... .
在 Visual Studio 2022 中,可以突出显示元组并将其更改为结构。例如:
public List<(string opName, string fileName)> GenerateTable()
右击selectQuick Actions and Refactorings
(或CTRL.)出现一个可以改到一个结构。
上下文
我继承了一个大量使用 Tuple<...> 类型的遗留代码库。代码充满了这样的行:
myVar = myTuple.Item4;
我想通过引入具有描述性 属性 名称的自定义 类 来重构此代码以提高可读性。
问题
是否有任何工具可以有效支持此重构操作?
我认为这个答案不会完全符合您的期望,但既然我去过那里,我想我会分享我的做法。
不幸的是,我没有找到使用某些工具完全自动化此重构的方法。但是,我是这样处理这个问题的...
如果用元组来表示你现在要介绍的单个class,那你就走运了。我想情况并非如此,因此您将不得不进行一些分类。
但首先要做的是...
寻找元组
我查找了所有使用Tuple
的地方。
独立 Visual Studio 和 Resharper 都可以通过使用 Find Usages 或 Find Usages Advanced(如果使用 Tuple.Create()
).
否则或另外,您将需要搜索 new Tuple<
或 Tuple
.
根据这些信息,确定需要介绍的第一个 class。
1。创建 class
... 现在让它看起来像 Tuple
。 XML-Doc 可以在稍后的重构中处理 class 时提供帮助。
class ClassThatWasMissing
{
/// <summary>
/// Represents some Property.
/// </summary>
int Item1 { get; set; }
/// <summary>
/// Represents some other Property.
/// </summary>
string Item2 { get; set; }
}
2。用 ClassThatWasMissing
的声明和初始化替换 Tuple
-creation
- Tuple<int, string> myvar = new Tuple<int, string>();
+ ClassThatWasMissing myvar = new ClassThatWasMissing();
myvar.Item1 = 42;
myvar.Item2 = "Some information";
3。重构 ClassThatWasMissing
为属性 (Item{n}
) 提供更具描述性的名称。上面的评论此时就可以派上用场了。
同样,Resharper 和独立 Visual Studio 都可以提供帮助(例如 R#:重构 → 重命名):
class ClassThatWasMissing
{
/// <summary>
/// Represents some Property.
/// </summary>
int SomeProperty { get; set; }
/// <summary>
/// Represents some other Property.
/// </summary>
string SomeOtherProperty { get; set; }
}
在这一点上还要记住,有时在使用点而不是在声明点应用名称可能更容易,因为它们的目的在这些点可能更明显。 VS 和 R# 都可以从那里重命名属性。
ClassThatWasMissing myvar = new ClassThatWasMissing();
myvar.SomeProperty = 42;
myvar.SomeOtherProperty = "Some information";
重复该过程
...对于您最初或连续找到的所有地点。这样做直到没有 Tuple
剩余(或至少只有您可以忍受的)。
祝你好运!
(2021年访题)
如果可用,您可以重构为 c# 7+ Tuple types。
我认为这个过程有两个阶段:
- 正则表达式从
Tuple<Type1, Type2 ...>
到(Type1 Item1, Tupe2 Item2 ...)
的声明
- 重命名(例如使用 VS 的
Crtl+R
、Ctrl+R
)从Item1
到AmazingName
、Item2
到EvenBetterName
、... .
在 Visual Studio 2022 中,可以突出显示元组并将其更改为结构。例如:
public List<(string opName, string fileName)> GenerateTable()
右击selectQuick Actions and Refactorings
(或CTRL.)出现一个可以改到一个结构。