dotnet/roslyn

Add refactorings to explicitly generate implicit record members

開放

#81,126 建立於 2025年11月10日

 (3 則留言) (0 個反應) (0 位負責人)C# (4,257 個分叉)batch import
Area-IDEhelp wanted

倉庫指標

星標
 (20,414 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

Summary

Add refactorings that will explicitly create implicitly declared record members, like ToString, PrintMembers etc.

Background and Motivation

Records declare implicitly some members that we can override with our own logic, which are Equals(T), GetHashCode, ToString and PrintMembers. However, when the logic needs to be overridden, one has to write these methods from scratch, even if just changing equality logic for a single property or changing the way one member is printed in PrintMembers.

Proposed Feature

Add refactorings for each of the overridable record's implicitly declared methods that would generate them explicitly. Generated methods should do the same as the implicit ones do. It will make it easier to override logic as we'd have the original method that we'd be able to augment instead of writing from scratch.

Alternative Designs

This can still be done manually. For some cases of overriding Equals you could go with making a wrapper type for the value involved which defines it's own equality. Also, a source generator could be created that helps managing these methods, but this is something that wouldn't be done in roslyn.

貢獻者指南