Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Feature] Add value sanitizers to [ObservableProperty]

Đang mở
#1,067 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu từ trình tạo mã nguồn [ObservableProperty] và setter được tạo của nó, sử dụng các ví dụ trong issue này làm cơ sở hành vi. Trước tiên, hãy xác định API của sanitizer, các thao tác được hỗ trợ và phạm vi của setter/getter; được xem là hoàn tất khi thiết kế đã thống nhất hỗ trợ các trường hợp sanitizer được yêu cầu và các property được tạo áp dụng chúng chính xác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

feature request :mailbox_with_mail:
Overview

In many of my properties I must Clamp, Round, Max or Min the setter value.
This auto defeats the [ObservableProperty] which make me implement the raw property.
Example:

public double TimeoutSeconds
{
    get;
    set => SetProperty(ref field, Math.Round(
        Math.Clamp(value, 0, 10000),
        2,
        MidpointRounding.AwayFromZero)
    );
} = 5000;

Example of what I would like:

[ObservableProperty]
[ObservablePropertySetterClamp(0, 10000)]
[ObservablePropertySetterRound(2, MidpointRounding.AwayFromZero)]
public partial double TimeoutSeconds { get; set; }
API breakdown

Not sure how to make the API but I provide a sample of generated code:

[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.4.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial global::System.Double TimeoutSeconds
{
    get => field;
    set
    {
        // The new sanitizer magic here
        value = Math.Clamp(value, 0, 10000);
        value = Math.Round(value, 2, MidpointRounding.AwayFromZero);
        // End

        // Current implementation, unchanged.
        if (!global::System.Collections.Generic.EqualityComparer<global::System.Double>.Default.Equals(field, value))
        {
            OnLastExecutedDateTimeChanging(value);
            OnLastExecutedDateTimeChanging(default, value);
            field = value;
            OnLastExecutedDateTimeChanged(value);
            OnLastExecutedDateTimeChanged(default, value);
            OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.LastExecutedDateTime);
        }
    }
}
Usage example
[ObservableProperty]
[ObservablePropertySetterClamp(0, 10000)]
[ObservablePropertySetterRound(2, MidpointRounding.AwayFromZero)]
public partial double TimeoutSeconds { get; set; }
[ObservableProperty]
[ObservablePropertySetterMin(0)]
public partial int LapCount { get; set; }
[ObservableProperty]
[ObservablePropertySetterStringTrim]
[ObservablePropertySetterStringUpperCase]
public partial string Country { get; set; }
[ObservableProperty]
[ObservablePropertySetterCustom(MyStaticSanitizerCallback)]
public partial string MySecretString { get; set; }
Breaking change?

No

Alternatives

The team can find a better way and/or names to do such, right now I don't see another alternative than using atributes.

Additional context
  • Also implement other sanitizers useful to other uses and types
  • If doing for setter we can also do for getter side?
Help us help you

Yes, I'd like to be assigned to work on this item

Ngôn ngữ chính
C#
Star
3.8k
Fork
401
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của CommunityToolkit/dotnet

Tất cả issue của CommunityToolkit/dotnet

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.