开发者

Declaring parameter that i don't want to pass

 public Jquery Extra(this HtmlHelper htmlhelper, 
                     string message, 
                     IDictionary<string, object> htmlAttributes)

if i declare the this Htmlhelper htmlhelper when i declare开发者_开发百科 my method, but i don't want to pass that parameter in when i call the method??

am i making sense


I believe you are trying to write an Extension Method. You define it like so

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static Jquery Extra(this HtmlHelper htmlhelper, string message, IDictionary htmlAttributes)
        {
            //do work
            return Jquery;
        }
    }   
}

And then use it like this:

HtmlHelper helper = new HtmlHelper();
Jquery jq = helper.Extra(message, htmlAttributes);


EDIT: It sounds like you want to be able to call this method without any HtmlHelper object at all.

If the method needs an HtmlHelper, you will not be able to call it without one.
You should rewrite the method so that it doesn't need an HtmlHelper.


You can make an overload with fewer parameters:

public static Jquery Extra(this HtmlHelper htmlhelper, string message) {
    return htmlHelper.Extra(message, null);
}

In C# 4, you can also use an optional parameter:

public Jquery Extra(this HtmlHelper htmlhelper, string message, IDictionary<string, object> htmlAttributes = null) {

I highly recommend that you also add an overload that takes an anonymous type:

public static Jquery Extra(this HtmlHelper htmlhelper, string message, object htmlAttributes) {
    return htmlHelper.Extra(message, null, new RouteValueDictionary(htmlAttributes));
}


Who is the author of this function? If it's you then do not include the first parameter. public Jquery Extra(string message, IDictionary<string, object> htmlAttributes).
If it's code you have not written yourself, then the HtmlHelper variable is probably necessary, and you shoud not attempt to remove it from the function prototype.
One of your comments said that you cannot initialize a HtmlHelper, that is not technically true. See the [msdn reference].1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜