c# - LINQ扩展方法求助二

标签 c# .net asp.net-mvc

有没有办法让这个扩展方法的参数在我看来“智能感知”?

目前,我可以获得有关参数(在 Controller 操作方法中)是什么的工具提示,但我很乐意自信地在 IntelliSense 中输入“安全”参数名称。反正废话不多说,先说方法,再说用法:

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

用法(其中 FundShareholderController 是我的 Controller ,x.JsFile() 是其上的操作方法。):

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

我希望这是有道理的。如果有任何遗漏的细节需要帮助,请告诉我。

顺便说一句 - 任何优化技巧也很乐意采纳。

最佳答案

尝试在方法上添加 XML 注释,如下所示。

/// <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>

将上面的注释放在扩展方法上后,我可以在 IntelliSense 中看到它们。请参见下图。

intellisense

关于c# - LINQ扩展方法求助二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3921874/

相关文章:

asp.net - AntiForgery Token 使用 ASP.NET5 Web API,在 NET46 上没有 System.Web.Helpers

c# - 为什么 ProtoBuf-Net 在序列化字符串列表时性能如此差?

c# - 带有 asp.net 的 sql server 的动态下拉功能

c# - 用于迭代集合并编辑 C# 的 Lambda 表达式

.net - 从 VB.net 调用时 native c++ dll 运行速度比从 native .exe 调用时运行速度慢

asp.net-mvc - 将 JWT token 存储在 MVC 5 的 cookie 中

c# - 如何在子键分组时使用 LINQ 连接两个父/子对象列表

.net - 本土集成系统还是 ESB?

.net - 模块化产品的最佳 .NET 框架/方法?

asp.net-mvc - 如何在 ASP.Net MVC 中读取文件的内容?