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

C#asp.net MvcResharperIntellisenseHtml Helper

C# Problem Overview


[Enable intellisense on HTMLHelper attribute for css classes]

I have this HTMLhelper:

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

I want Resharper to give me IntelliSense for CSS classes defined in my application when passing the value for the "cssClass" parameter.

There are some code annotation attributes that Resharper recognizes, but none that seem directly related to marking a method parameter as being CSS classes.

The closest I could find was [HtmlAttributeValue(string name)]. I tried to apply to the cssClass parameter like this:

public IHtmlString MyTextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TProperty>> propertyExpression, 
    [HtmlAttributeValue("class")] string cssClass)
{
    // ...
}

But that doesn't work. It would also be super awesome if Resharper would recognize the entered class and stop bugging me about unknown CSS classes in jQuery selector expressions (that operate on the textbox generated by the helper above).

Edit: Here's a screenshot of the kind of intellisense that is working for the "htmlAttributes" parameter of an action method. This is accomplished by using the [HtmlElementAttributes] annotation on the parameter.

Resharper htmlAttributes intellisense

I want a similar annotation that lets me put css classes in a string parameter and have the same intellisense appear showing css classes.

C# Solutions


Solution 1 - C#

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);
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaif AtefView Question on Stackoverflow
Solution 1 - C#FabView Answer on Stackoverflow