Proper way to put attributes in generated properties when using sour generators #217
Answered
by
michael-hawker
cesarchefinho
asked this question in
Q&A
|
What is the proper way to preserve attributes of observable properties when using source generators? Example private string? firstName;
[Required]
[CustomValidatorBlaBla]
public string? FirstName
{
get => firstName;
set
{
if (SetProperty(ref firstName, value))
{
OnPropertyChanged(nameof(FullName));
GreetUserCommand.NotifyCanExecuteChanged();
}
}
}
private string? lastName;
public string? LastName
{
get => lastName;
set
{
if (SetProperty(ref lastName, value))
{
OnPropertyChanged(nameof(FullName));
GreetUserCommand.NotifyCanExecuteChanged();
}
}
}
public string? FullName => $"{FirstName} {LastName}";Whne using generators we code as bellow [ObservableProperty]
[AlsoNotifyChangeFor(nameof(FullName))]
[AlsoNotifyCanExecuteFor(nameof(GreetUserCommand))]
private string? firstName;
[ObservableProperty]
[AlsoNotifyChangeFor(nameof(FullName))]
[AlsoNotifyCanExecuteFor(nameof(GreetUserCommand))]
private string? lastName;
public string? FullName => $"{FirstName} {LastName}";How I make Required and CustomValudatorBlaBla be applied to FirstName property generetaded? |
Answered by
michael-hawker
May 6, 2022
Replies: 3 comments 3 replies
|
You can just use the attribute, and it'll be automatically ported over to the generated property 🙂 |
3 replies
|
I resolve this issue using reflection and reading private fields attributes with same name of propretyes and uncaptalize the first letter of name. |
0 replies
Answer selected by
michael-hawker
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tracked by issue #208