RefactorMCP catalog

Move Member to Another Partial File

Moves a member of a partial type into the part of that type declared in another file, or into a new file holding a new part.

Arguments

ArgumentMeaning
target.symbolthe member to move, such as M:Shop.Order.Discount(System.Decimal)
filethe file to move it to, relative to the project root of the case

Precondition

  • The member's containing type is declared partial.
  • The target file is not the member's own file.
  • The target file, when it exists, declares a part of the same type.
  • When the target file does not exist, the type is not nested.

Transformation

  • The member, with the comments above it, is removed from its part and appended to the end of the other part, separated by a blank line.
  • A field declared alongside others (decimal _total, _discount;) is split out and moved on its own.
  • A new file repeats the source file's usings and namespace form (block or file-scoped) and declares a part with the type's modifiers, keyword, name and type parameters, but without base types, constraints or attributes, which the original part keeps.
  • The target file gains the usings the member needs; the source file drops those only the member needed.

Preserved

  • The type, its members and their meaning: parts of a partial type compile to one type.
  • The member's body, comments and documentation.

Limitations

  • The member is always appended at the end of the target part, not placed among members of its kind.
  • Partial methods, which have a declaration and an implementation, move one declaration at a time.

Error codes

CodeMeaning
type-not-partialthe containing type is not partial
same-filethe target file is the member's own file
no-part-in-filethe target file exists but declares no part of the type
nested-typea new file was asked for a nested type's member

Cases

·

Diffs show each file from before to after. /*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.

#field

A field declared alongside another is split out of its declaration and moved on its own

success
Targetsymbol F:Shop.Order._discount
Arguments
file"Order.Pricing.cs"
Order.Pricing.csmodified
11 namespace Shop
22 {
33 public partial class Order
44 {
55 public void Discount(decimal amount) => _discount = amount;
6+
7+ private decimal _discount;
68 }
79 }
Order.csmodified
11 namespace Shop
22 {
33 public partial class Order
44 {
5− private decimal _total, _discount;
5+ private decimal _total;
66
77 public decimal Net => _total - _discount;
88 }
99 }

#generic-type

A member of a generic partial class moves to a new part declared with the same type parameters

success
Targetsymbol M:Shop.Repository`1.Clear
Arguments
file"Repository.Maintenance.cs"
Repository.Maintenance.csadded
1+namespace Shop
2+{
3+ public partial class Repository<T>
4+ {
5+ public void Clear()
6+ {
7+ _items.Clear();
8+ }
9+ }
10+}
Repository.csmodified
11 using System.Collections.Generic;
22
33 namespace Shop
44 {
55 public partial class Repository<T> where T : class
66 {
77 private readonly List<T> _items = new List<T>();
88
99 public void Add(T item) => _items.Add(item);
10−
11− public void Clear()
12− {
13− _items.Clear();
14− }
1510 }
1611 }

#no-part-in-file-rejected

Refuses an existing file that declares no part of the class

refusal
Targetsymbol M:Shop.Order.Total
Arguments
file"Customer.cs"
Refusesno-part-in-file; every file is left unchanged
Customer.csinput
1 namespace Shop
2 {
3 public class Customer
4 {
5 }
6 }
Order.csinput
1 namespace Shop
2 {
3 public partial class Order
4 {
5 public decimal Total() => 0;
6 }
7 }

#not-partial-rejected

Refuses a member of a class that is not partial

refusal
Targetsymbol M:Shop.Order.Total
Arguments
file"Order.Pricing.cs"
Refusestype-not-partial; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 public decimal Total() => 0;
6 }
7 }

#same-file-rejected

Refuses to move a member to the file it is already in

refusal
Targetsymbol M:Shop.Order.Total
Arguments
file"Order.cs"
Refusessame-file; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public partial class Order
4 {
5 public decimal Total() => 0;
6 }
7 }

#to-existing-part

Moves a method, with the comment above it, to the end of another part of its partial class

success
Targetsymbol M:Shop.Order.Discount(System.Decimal)
Arguments
file"Order.Pricing.cs"
Order.Pricing.csmodified
11 namespace Shop
22 {
33 public partial class Order
44 {
55 public decimal Total => _total;
6+
7+ // Discounts never take the total below zero.
8+ public decimal Discount(decimal amount)
9+ {
10+ return amount > _total ? 0 : _total - amount;
11+ }
612 }
713 }
Order.csmodified
11 namespace Shop
22 {
33 public partial class Order
44 {
55 private decimal _total;
66
7− // Discounts never take the total below zero.
8− public decimal Discount(decimal amount)
9− {
10− return amount > _total ? 0 : _total - amount;
11− }
12−
137 public void Add(decimal price) => _total += price;
148 }
159 }

#to-new-file

Creates the target file with a new part of the class, in the same namespace form, when it does not exist yet

success
Targetsymbol P:Shop.Order.IsEmpty
Arguments
file"Order.Queries.cs"
Order.Queries.csadded
1+namespace Shop;
2+
3+public partial class Order
4+{
5+ public bool IsEmpty => _lines.Count == 0;
6+}
Order.csmodified
11 using System.Collections.Generic;
22
33 namespace Shop;
44
55 public partial class Order
66 {
77 private readonly List<string> _lines = new List<string>();
8−
9− public bool IsEmpty => _lines.Count == 0;
108 }

#usings-follow-the-member

The target part's file gains the usings the member needs, and the source file drops those only the member needed

success
Targetsymbol M:Shop.Order.Skus
Arguments
file"Order.Queries.cs"
Order.Queries.csmodified
1+using System.Collections.Generic;
2+using System.Linq;
3+
14 namespace Shop
25 {
36 public partial class Order
47 {
58 public int Count => _lines.Count;
9+
10+ public IEnumerable<string> Skus() => _lines.Select(l => l.Sku).Distinct();
611 }
712 }
Order.csmodified
11 using System.Collections.Generic;
2−using System.Linq;
32
43 namespace Shop
54 {
65 public partial class Order
76 {
87 private readonly List<Line> _lines = new List<Line>();
9−
10− public IEnumerable<string> Skus() => _lines.Select(l => l.Sku).Distinct();
118 }
129
1310 public class Line
1411 {
1512 public string Sku { get; set; }
1613 }
1714 }