Melissa
Melissa

Reputation: 473

Add completion suggestion 'nameof' for parameter with Resharper

I would like to add a completion suggestion of nameof to a parameter in a method.

The method:

public static class Ensure
{
    /// <summary>
    /// throws <see cref="ArgumentException"/> exception if string is null or empty.
    /// </summary>
    /// <param name="value">string to check its content.</param>
    /// <param name="name">name of parameter passed.</param>
    public static void StringNotNullOrEmpty(string value, string name)
    {
        if (string.IsNullOrEmpty(value))
        {
            throw new ArgumentException("Value cannot be null or empty", name);
        }
    }
}

When typing Ensure.StringNotNullOrEmpty(testString, <...>) I would like to get the nameof(testString) suggestion for <...>. The above method doesn't suggest the nameof (Worse still, after typing 'n' I only get suggestions as null, new). The nameof(...) is necessary so I get the correct ParamName in the ArgumentException.

On the site of Resharper it says that ArgumentNullException makes use of the nameof suggestion by using the InvokerParameterNameAttribute (source: Resharper InvokerParameterNameAttribute).

Attempt: So I installed the nuget package in my project: JetBrains.Annotations 10.4.0 and changed the method as followed:

public static class Ensure
{
    /// <summary>
    /// throws <see cref="ArgumentException"/> exception if string is null or empty.
    /// </summary>
    /// <param name="value">string to check its content.</param>
    /// <param name="name">name of parameter passed.</param>
    public static void StringNotNullOrEmpty([CanBeNull] string value, 
        [NotNull] [InvokerParameterName] string name)
    {
        if (string.IsNullOrEmpty(value))
        {
            throw new ArgumentException("Value cannot be null or empty", name);
        }
    }
}

Unfortunately the above attempt is still not giving me the suggestion of nameof when I've typed: Ensure.StringIsNotNullOrEmpty(testString,.

Some other thing I found was the CallerMemberNameAttribute on MSDN. But this only gives me the name of the calling method as a string.

I also looked into some repo's on GitHub, and could only find solutions the same way as my attempt.

One reason I can think of is when having two string parameters in a method with an InvokerParameterNameAttribute defined on the latter, it could be confusing for Resharper.

Is it possible to add the nameof suggestion for a parameter in a method? And how can I achieve this with the above method?

Additional info: I have VS2015 + Resharper 2016.3.2.

Upvotes: 0

Views: 454

Answers (1)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

actually that works (but not as you intended). it will not suggest as soon as you type ,... it will only suggest you to use nameof expression when you fully type name of argument in form of string instead of using nameof expression.

static void Caller(string arg)
{
    StringNotNullOrEmpty(arg, "arg"); // only now resharper suggests to use nameof.
    StringNotNullOrEmpty(arg, "something else"); // now resharper complains about unknown argument name.
}

You can submit feedback on resharper to actually implement the behavior you want.

Also if you type "" the cursor will move to middle of double quotes and resharper suggests name of available parameters.

BTW you can always comment your code that can be useful for every future readers

/// <summary>
/// throws <see cref="ArgumentException"/> exception if string is null or empty.
/// </summary>
/// <param name="value">string to check its content.</param>
/// <param name="name">name of parameter passed.</param>
public static void StringNotNullOrEmpty([CanBeNull] string value,
    [NotNull] [InvokerParameterName] string name)
{
    if (string.IsNullOrEmpty(value))
    {
        throw new ArgumentException("Value cannot be null or empty", name);
    }
}

Upvotes: 1

Related Questions