Extension methods not showing

三世轮回 提交于 2019-12-07 13:51:27

问题


I am creating extension methods for the HtmlHelper class in an MVC web app. Nothing is showing, not even the default InputExtensions.

public static class HtmlHelpers
{
    public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script)
    {
        if (!RegisteredScriptIncludes.ContainsValue(script))
        {
            RegisteredScriptIncludes.Add(RegisteredScriptIncludes.Count, script);
        }
    }

    public static string RenderScripts(this HtmlHelper htmlhelper)
    {
        var scripts = new StringBuilder();
        foreach (string script in RegisteredScriptIncludes.Values)
        {
            scripts.AppendLine("<script src='" + script + "' type='text/javascript'></script>");
        }
        return scripts.ToString();

    }

    private static SortedList<int, string> RegisteredScriptIncludes
    {
        get
        {
            SortedList<int, string> value = (SortedList<int, string>)HttpContext.Current.Items["RegisteredScriptIncludes"];
            if (value == null)
            {
                value = new SortedList<int, string>();
                HttpContext.Current.Items["RegisteredScriptIncludes"] = value;
            }
            return value;
        }
    }

}

The extension methods are not showing in the code either.

Where are they?


回答1:


Did you forget a using statement? Specifically you'd need "using path.to.my.namespace;" to get the extension methods in.




回答2:


Another obvious thing to check for, in case anyone finds this post, is forgetting the "this" keyword in the first argument of your extension method. If you forget that, there isn't much the compiler can do to help you!



来源:https://stackoverflow.com/questions/992135/extension-methods-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!