Please allow partial properties #3412
Replies: 23 comments 36 replies
-
Can you explain why they would be great? What use cases would they solve? How would the syntax work? What does a "partial" getter do? Partial methods have some fairly strict rules. They exist for the purpose of generated/designer code to expose specific optional integration points. That's why partial methods must be private and must not return a value. It's entirely optional to provide an implementation, and if that implementation is not provided those partial method invocations are erased. None of that seems to make sense with properties. |
Beta Was this translation helpful? Give feedback.
-
The team is planning partial properties for a future version of C#, but decided there wasn't time for C# 9. |
Beta Was this translation helpful? Give feedback.
-
The use case I have is that I need to be able to add metadata to auto-generated properties. |
Beta Was this translation helpful? Give feedback.
-
@TonyValenti You might be able to use the TypeDescriptor stuff for that, although it's less convenient. Another great use-case would be for implementing INotifyPropertyChanged using source generators. |
Beta Was this translation helpful? Give feedback.
-
Apparently not after all. See notes at #3413 |
Beta Was this translation helpful? Give feedback.
-
What kind of metadata do you mean? Attributes? |
Beta Was this translation helpful? Give feedback.
-
The notes there are honestly worded far more strongly than I remember our decision being. |
Beta Was this translation helpful? Give feedback.
-
Me as well. |
Beta Was this translation helpful? Give feedback.
-
In my opinion, partial properties would be a very good fit with source code generators in a variety of cases where the code for a property is used repeatedly. The most obvious case are ViewModels, where you have a lot of properties that basically do the same: get and set the value and call the Writing this inside a class deriving from each library's view model base class: Could allow generation of this code:
|
Beta Was this translation helpful? Give feedback.
-
That is literally one of the first examples they give for source code generators - you supply an attribute-tagged backing field and it produces the property for you. That way, you don't need a partial property. [AutoNotify("MyProperty")]
private string _myProperty; |
Beta Was this translation helpful? Give feedback.
-
I believe the having the property on the human written code would allow the backing field to become a hidden implementation detail. It could be tagged to prevent it from appearing on intellisense thus making it very difficult to misuse the field instead of the property. Moreover, I don't see much difficulty in implement it, as they would be following the rules already laid for partial methods on C#9 (compulsory implementation, etc.) |
Beta Was this translation helpful? Give feedback.
-
Allowing for edition of existing code as proposed in #3772 would reduce the need for partial properties, as their implementation could be modified without being marked partial. |
Beta Was this translation helpful? Give feedback.
-
Another reason why partial properties will be superior for INPC than the approach of letting the user specify a backing field, and then having an SG which generates the property: it's hard to find references to a property which an SG has generated from a backing field. You need to find the SG-generated partial class (which can be quite a few clicks), then find the generated property within that, and then you can find all references. Frankly, it's easier to use Find in Files. If we had partial properties, you'd be able to just find all references there and then, rather than having to do digging. |
Beta Was this translation helpful? Give feedback.
-
I'm interested in helping to make partial properties happen, but I honestly don't know what the design would be. One issue I see is how to distinguish the "implementation" part of the property from the "declaration" part: // This partial property is implemented using a simple auto-property.
// but which part is the implementation?
public partial string Prop { get; set; }
public partial string Prop { get; set; } I guess you could introduce a modifier just for partial properties to convey that one of the parts is an "implementation" part. public partial string Prop { get; set; }
public auto partial string Prop { get; set; } You could also make it so at least initially, partial properties can't be implemented with auto properties. After all, it seems like the stuff people really want to accomplish with this feature doesn't involve auto-implementing the property. |
Beta Was this translation helpful? Give feedback.
-
I wonder whether partial events would be considered as well. It is quite useful when implementing async events and weak events. |
Beta Was this translation helpful? Give feedback.
-
For XAML frameworks like WPF or Avalonia it would be insanely useful to generate dependency properties. See discussion in Avalonia repo AvaloniaUI/Avalonia#6315 (comment)
Would auto generate:
|
Beta Was this translation helpful? Give feedback.
-
Definitely would love having partial properties too. We use attributes over fields in the MVVM Toolkit to generate observable properties, but it's really not a great experience at all. To name a few issues it has that would be resolved if we had partial properties:
Honestly having partial properties with the restriction that the implementation can't be an auto-property seems perfectly reasonable to me, and it would solve the issue of determining which declaration is the implementation and which one is the partial one. But really, having partial properties is really needed in scenarios where source generators are involved, such as this one. Really hope the LDM will consider this again maybe for C# 12, at this point 🙂 |
Beta Was this translation helpful? Give feedback.
-
Another use case is cross-platform code. Currently MAUI recommends implementing it with But what if I want a cross-platform property? Here's an example that changes app theme on Android: // Shared code
public partial class NightModeService
{
public partial NightModeStyle DefaultNightMode { get; set; }
}
public enum NightModeStyle
{
Unspecified = -100,
FollowSystem = -1,
No = 1,
Yes = 2,
AutoBattery = 3
}
// Platform-specific code
public partial class NightModeService
{
public partial NightModeStyle DefaultNightMode
{
get => (NightModeStyle)AppCompatDelegate.DefaultNightMode;
set => AppCompatDelegate.DefaultNightMode = (int)value;
}
} |
Beta Was this translation helpful? Give feedback.
-
I've come across another slightly subtle issue, which partial properties would solve nicely. The motivating example is an INPC source generator, where the user writes a backing field and the SG generates the corresponding property. A common feature of such tools is to be able to automatically generate a PropertyChanged event for one property when a property that it depends on changes, e.g.: partial class C
{
[Notify] private string _foo; // Generates the Foo property
public string Bar => Foo.ToUpperCase();
} Here, we want to raise a PropertyChanged notification for The problem is that when the SG runs, This gets even more complex if you start trying to support things like: partial class Parent
{
[Notify] Child _child;
public string ChildName => Child.Name;
} Partial properties would solve this nicely, because the syntax tree that the SG sees would have been properly bound. |
Beta Was this translation helpful? Give feedback.
-
I too would like to have partial properties. I'd like to generate json metadata for STJ and Newtonsoft at the same time using partial classes and partial properties. Like so: public partial class Entity
{
public partial int Id { get; set; }
} Generated code: public partial class Entity
{
[System.Text.Json.Serialization.JsonPropertyName("id")]
[Newtonsoft.Json.JsonProperty("id")]
public partial int Id { get; set; }
} |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Opened #6420. |
Beta Was this translation helpful? Give feedback.
-
With partial properties I'd be able to recreate this library in Roslyn https://github.com/mrpmorris/Morris.MetaMerge |
Beta Was this translation helpful? Give feedback.
-
The above would be great. Just needed it and never knew we only got partial classes and methods.
Beta Was this translation helpful? Give feedback.
All reactions