[ObservableValidator] Crash on NativeAOT published app when using custom validator
Describe the bug
If an app is published with NativeAOT active and it has custom validators, the app would crash when trying to validate the property. Debugging the NativeAOT app returns the error "The type <ViewModel> does not contain a public property named <Property>."
Regression
No response
Steps to reproduce
1. Create a new UI app that can be built with NativeAOT (for example, Avalonia).
2. Enable `PublishAot`
<PublishAot>true</PublishAot>
3. Create a UI that has a text box.
4. In the view model:
public sealed partial class MainWindowViewModel : ObservableValidator {
[ObservableProperty]
[IsValidPath(true)]
[NotifyDataErrorInfo]
string _sourceLocation;
}
6. Create a simple validator, like this:
public sealed class IsValidPathAttribute : ValidationAttribute
{
private readonly bool _isRequired;
public IsValidPathAttribute(bool required)
{
_isRequired = required;
}
public override bool IsValid(object value)
{
string potentialPath = value?.ToString();
if (!string.IsNullOrEmpty(potentialPath) || !string.IsNullOrWhiteSpace(potentialPath))
{
foreach (char invalidChar in Path.GetInvalidPathChars())
{
if (potentialPath.Contains(invalidChar))
{
ErrorMessage = "The path to the file shouldn't have invalid characters.";
return false;
}
}
if (!Directory.Exists(potentialPath) || !(Path.IsPathRooted(potentialPath) && !Path.GetPathRoot(potentialPath).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)))
{
ErrorMessage = "The path doesn't exist or isn't valid.";
return false;
}
return true;
}
if (_isRequired)
{
ErrorMessage = "This is required.";
}
return !_isRequired;
}
}
7. Bind the property to the text box and run.
8. Run the following command in the CLI (while pointing to the project's .csproj file):
dotnet publish -a x64 --self-contained
8 Run the published executable.
Expected behavior
The app should run normally and do the proper validation.
Screenshots
No response
IDE and version
VS 2022
IDE version
17.8.3
Nuget packages
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)
8.2.2
Additional context
No response
Help us help you
Yes, but only if others can assist
2 years later, I have the same issue but not just with custom validators. With any validation. Even a simple code like this:
[ObservableProperty]
[NotifyDataErrorInfo]
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Please enter a valid email address")]
private string? email;
will produce "the type bla does not contain a public property called email" exception.
A workaround I got from chatgpt is to create a XML file, called rd.xml for example
<linker>
<assembly fullname="yourassemblyname">
<type fullname="yourassemblyname.ViewModels.yourviewmodel" preserve="all" />
</assembly>
</linker>
then in your csproj:
<ItemGroup>
<TrimmerRootDescriptor Include="rd.xml" />
</ItemGroup>
Then validation works in AOT. Not sure if @FirehawkV21 is still looking for a solution after so many time but leaving this for others having the same issue and looking for a solution. Doesnt look like its getting fixed any time soon.
Can confirm that @Symbai 's workaround does work. I do hope that Mvvm Toolkit gets an update that fully supports AOT with data validators (I think .NET 10 does have support for that scenario).