CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

[ObservableValidator] Crash on NativeAOT published app when using custom validator #810

Open FirehawkV21 opened 11 months ago

FirehawkV21 commented 11 months ago

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

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, but only if others can assist