RefactorMCP catalog

Make Type Partial

Adds the partial modifier to a class, struct, record or interface, so its members can be spread over several declarations.

Arguments

None. The target is the type, by symbol.

Precondition

  • The type is a class, struct, record, record struct or interface. Enums and delegates cannot be partial.
  • The type is not already partial.

Transformation

  • partial is added as the last modifier, immediately before the class, struct, record or interface keyword, where C# requires it.
  • A declaration with no modifiers gets partial in front of its keyword, after its attributes, documentation comment and any comment above it.
  • A containing type is left alone: a nested type can be partial on its own.

Preserved

  • The type's meaning: a single partial declaration compiles to the same type.
  • Attributes, comments, documentation, type parameters and constraints.

Limitations

  • No new part is created; adding members in another file is a separate step, such as Move Member to Another Partial File.

Error codes

CodeMeaning
already-partialthe type is already declared partial
unsupported-type-kindthe type is an enum or a delegate

Cases

·

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

#already-partial-rejected

Refuses a type that is already partial

refusal
Targetsymbol T:Shop.Order
Refusesalready-partial; every file is left unchanged
Order.csinput
1 namespace Shop;
2
3 public partial class Order
4 {
5 public int Id { get; set; }
6 }

#enum-rejected

Refuses an enum, which cannot be partial

refusal
Targetsymbol T:Shop.Status
Refusesunsupported-type-kind; every file is left unchanged
Status.csinput
1 namespace Shop;
2
3 public enum Status
4 {
5 Open,
6 Closed,
7 }

#generic-struct

Adds partial to a generic readonly struct with a constraint

success
Targetsymbol T:Shop.Range`1
Range.csmodified
11 using System;
22
33 namespace Shop;
44
5−public readonly struct Range<T> where T : IComparable<T>
5+public readonly partial struct Range<T> where T : IComparable<T>
66 {
77 public Range(T low, T high)
88 {
99 Low = low;
1010 High = high;
1111 }
1212
1313 public T Low { get; }
1414
1515 public T High { get; }
1616 }

#interface-implemented-in-another-file

Adds partial to an interface, leaving the class that implements it in another file unchanged

success
Targetsymbol T:Shop.IPricing
IPricing.csmodified
11 namespace Shop;
22
3−internal interface IPricing
3+internal partial interface IPricing
44 {
55 decimal Price(string sku);
66 }
FlatPricing.csunchanged
11 namespace Shop;
22
33 internal class FlatPricing : IPricing
44 {
55 public decimal Price(string sku) => 1m;
66 }

#nested-record

Adds partial to a nested record, leaving the containing class as it is

success
Targetsymbol T:Shop.Catalogue.Entry
Catalogue.csmodified
11 namespace Shop;
22
33 public class Catalogue
44 {
5− public record Entry(string Sku, decimal Price);
5+ public partial record Entry(string Sku, decimal Price);
66
77 public Entry First() => new Entry("A1", 2m);
88 }

#no-modifiers-with-documentation

Adds partial to a class with no modifiers, after its documentation comment, attribute and comment

success
Targetsymbol T:Shop.Basket
Basket.csmodified
11 using System;
22
33 namespace Shop
44 {
55 /// <summary>The items a customer is about to buy.</summary>
66 [Serializable]
77 // Kept small on purpose.
8− class Basket
8+ partial class Basket
99 {
1010 public int Count;
1111 }
1212 }

#sealed-class

Adds partial after the other modifiers of a sealed class

success
Targetsymbol T:Shop.Invoice
Invoice.csmodified
11 namespace Shop;
22
3−public sealed class Invoice
3+public sealed partial class Invoice
44 {
55 public decimal Total { get; set; }
66 }