Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ extension method help sought II

Is there a way to make the parameters of this extension method 'intellisensible' from my view?

At the moment, I can get a tooltip nudge of what the parameters (in the controller action method) are but would love to confidently IntelliSense type the parameter names for 'safety'. Anyway, without further ado, the method, followed by the usage:

public static string Script<T>(this HtmlHelper html,
                                Expression<Action<T>> action) where T:Controller
{
    var call = action.Body as MethodCallExpression;

    if (call != null)
    {
        // paramDic - to be used later for routevalues
        var paramDic = new Dictionary<string, object>();
        string actionName = call.Method.Name;
        var methodParams = call.Method.GetParameters();

        if (methodParams.Any())
        {
            for (int index = 0; index < methodParams.Length; index++)
            {
                ParameterInfo parameterInfo = methodParams[index];
                Expression expression = call.Arguments[index];
                object objValue;
                var expressionConst = expression as ConstantExpression;
                if(expressionConst!=null)
                {
                    objValue = expressionConst.Value;
                }
                else
                {
                    Expression<Func<object>> expressionConstOther =
                        Expression.Lambda<Func<object>>(
                          Expression.Convert(expression, typeof(object)),
                          new ParameterExpression[0]);
                    objValue = expressionConstOther.Compile()();
                }
                paramDic.Add(parameterInfo.Name, objValue);
            }
        }
        string controllerName = typeof(T).Name;
        if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
        {
            controllerName = controllerName.Remove(controllerName.Length - 10, 10);
        }

        var routeValues = new RouteValueDictionary(paramDic);
        var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        var url = urlHelper.Action(actionName, controllerName, routeValues);

        const string linkFormat = "<script type=\"text/javascript\" src=\"{0}\"></script>";
        string link = string.Format(linkFormat, url);

        return link;
    }
    return null;
}

Usage (where FundShareholderController is my controller and x.JsFile() is an action method thereon.):

<%=Html.Script<FundShareholderController>(x => x.JsFile("CreateInvestorBookingJsFile", 0))%>

I hope this makes sense. Let me know if there are any missing details that you need to assist.

BTW - any optimization tips are gladly taken on-board too.

like image 733
jim tollan Avatar asked Oct 13 '10 08:10

jim tollan


People also ask

What is LINQ extension method?

Linq provides standard query operators like filtering, sorting, grouping, aggregation, and concatenations, and it has many operators to achive many types of functionalities, which are called extension methods, in LINQ.

Does LINQ use extension methods?

You extend the set of methods that you use for LINQ queries by adding extension methods to the IEnumerable<T> interface. For example, in addition to the standard average or maximum operations, you create a custom aggregate method to compute a single value from a sequence of values.

What is extension method in C# and how?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.


1 Answers

Try adding the XML comments on the method like the following.

/// <summary>
/// The summary of my Script Extension method.
/// </summary>
/// <typeparam name="T">T is the type of Controller</typeparam>
/// <param name="html">The HTML helper.</param>
/// <param name="action">The action Method of the Controller.</param>
/// <returns></returns>

After putting the above comments on the extension method, I can see them in the IntelliSense. See the image below.

intellisense

like image 177
Zain Shaikh Avatar answered Sep 28 '22 05:09

Zain Shaikh