Shimrod
Shimrod

Reputation: 3205

How to get the name of a parameter passed to a method?

I currently have a validation class with a method which validates that a string is not null (and logs the info, but that's not the concern here)

protected void ValidateStringNotEmpty(string propertyName, string value)
{
    if (string.IsNullOrWhiteSpace(value))
    {
        ValidationErrors.Add($"Property [{propertyName}] must be filled");
    }
}

which is called using

ValidateStringNotEmpty(nameof(Name), Name);

Is there an easy (and optimized) way to prevent the need to pass the first argument nameof(property) ?

I saw solutions using the stacktrace to trace the call back, but it does not seem to be a great solution...

EDIT: What I'm really trying to do is see if something exists to do this (removing the need to pass the name of the argument). I know the pragmatic solution is the one I already use, and this is not a problem.

Upvotes: 2

Views: 1224

Answers (2)

kipy
kipy

Reputation: 632

There is now "CallerArgumentExpression" that has shipped with dotnet 6 / C# 10 and doing exactly what you're looking for

protected void ValidateStringNotEmpty(string value, [CallerArgumentExpression("value")] string propertyName = "")

Upvotes: 4

aybe
aybe

Reputation: 16652

What about validating directly inside the property and leveraging [CallerMemberName] ?

private string _text;

public string Text
{
    get => _text;
    set
    {
        if (IsValid(value))
        {
            _text = value;
        }
        else
        {
            ValidationErrors.Add("'abcd' was expected.");
        }
    }
}

private bool IsValid<T>(T value, [CallerMemberName] string propertyName = null)
{
    switch (propertyName)
    {
        case nameof(Text):
            return value as string == "abcd";
        default:
            throw new ArgumentOutOfRangeException(nameof(propertyName), propertyName, null);
    }
}

public static class ValidationErrors
{
    public static void Add(string message, [CallerMemberName] string propertyName = null)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 0

Related Questions