Skip to content

Why does ValidationMessageStore Add have an issue with nameof saying it a constant expression #62496

Closed
@dgxhubbard

Description

@dgxhubbard

In .net 9

In validation of a blazor page on compile I get a runtime exception, Constant Expression, when using this statement

            MessageStore?.Add ( () => nameof ( Subscriber.Topic ), localMsg.TopicRequired );

Subscriber is a simple class

`
class Subscriber
{
public string Topic {get; set;}
public string ClientId {get; set;}
}

`

All that I want to do is let name of retrieve the name of the field, yet in order to do this I have to use the much more complicated statement

            MessageStore?.Add ( new FieldIdentifier ( Model, nameof ( Subscriber.Topic ) ), localMsg.NumberOfTopicsClientIdMustMatch );

Where Model is the blazor page model and is of type Subscriber.

Add takes a FieldIdentifier or Expression<Func> accessor. Why can't the add method use a simple nameof rather than the more complicated FieldIdentifier and the accessor method?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions