Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell Resharper that a method parameter is a string containing a CSS class?

Use [ValueProvider]

From the Code Annotations currently supported by Resharper 10, the best candidate would to use this attribute. From the above link:

ValueProviderAttribute

For a parameter that is expected to be one of the limited set of values. Specify fields of which type should be used as values for this parameter.

Unfortunately, I've not figured out how it works. Maybe it's buggy in my Resharper version 9.2.

What I've tried so far:

namespace ValueProviderSample
{
    public static class MyValuesContainer
    {
        public static readonly string[] Values = { "one", "two", "three" };
    }

    public class MyMethodContainer
    {
        public string MyMethod([ValueProvider("ValueProviderSample.MyValuesContainer.Values")]
                               string parameter)
        {
            return string.Empty;
        }
    }
}

Even if you make it work, you'll still have to populate the Values list.

And of course, you can still develop your code annotation/extension for Resharper.

Why not using a strong typed object instead of string ?

Sometimes instead of using string and int, we could use stronger-typed classes of our own design. Since it seems you control your code, you can instead of using string with a css name in it, you can create a new type like CssClass.

You just need to add as a prebuilt event a call to a generator which parses every css in the project and dynamically create a class like that:

public class CssClass
{
    public string Name { get; private set; }

    public static CssClass In = new CssClass("in");

    /// <summary>
    /// Initialise une nouvelle instance de la classe <see cref="T:System.Object"/>.
    /// </summary>
    private CssClass(string name)
    {
        Name = name;
    }
}

and then your sample will look like that:

public class MySample
{
    public IHtmlString MyTextBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> html,
        Expression<Func<TModel, TProperty>> propertyExpression,
        CssClass cssClass)
    {
        // ...
    }

    public void Usage()
    {
        MyTextBoxFor(html, expression, CssClass.In);
    }
}