0

I decided to help my friend with a project he's working on. I'm trying to write a test webpage for him to verify some new functionality, but in my auto-generated code I get

CS1106: Extension method must be defined in a non-generic static class

Implementing the code in index.cshtml isn't the best way to do this, but we are just trying to do a proof of concept and will do a proper implementation later.

In all the places I looked they pretty much said that all the functions I define must be in a static class (as the error states). That wouldn't be so bad except for the class that holds all my functions is auto-generated and not static. I'm not really sure what settings I can change to fix this.

Here is a copy of the relevant (I believe) parts of code. The implementation of some or all of the functions may be incorrect. I haven't tested them yet

@{
    HttpRequest req = System.Web.HttpContext.Current.Request;
    HttpResponse resp = System.Web.HttpContext.Current.Response;
    var url = req.QueryString["url"];

        //1 Download web data from URL

        //2 Write the final edited version of the document to the response object using resp.write(String x); 

        //3 Add Script tag for dom-outline-1.0 to html agility pack document

        //4 Search for relative URLs and correct them to become absolute URL's that point back to the hostname
}

@functions
    {
    public static void PrintNodes(this HtmlAgilityPack.HtmlNode tag)
    {
        HttpResponse resp = System.Web.HttpContext.Current.Response;
        resp.Write(tag.Name + tag.InnerHtml);

        if (!tag.HasChildNodes)
        {
            return;
        }
        PrintNodes(tag.FirstChild);
    }

    public static void AddScriptNode(this HtmlAgilityPack.HtmlNode headNode, HtmlAgilityPack.HtmlDocument htmlDoc, string filePath)
    {
        string content = "";

        using (StreamReader rdr = File.OpenText(filePath))
        {
            content = rdr.ReadToEnd();
        }
        if (headNode != null)
        {
            HtmlAgilityPack.HtmlNode scripts = htmlDoc.CreateElement("script");
            scripts.Attributes.Add("type", "text/javascript");
            scripts.AppendChild(htmlDoc.CreateComment("\n" + content + "\n"));
            headNode.AppendChild(scripts);
        }
    }
}
<HTML CODE HERE>
myselfesteem
  • 733
  • 1
  • 6
  • 23
  • 3
    Well why do you *want* to do this in an autogenerated class? Move it out of your page, and into a normal .cs file. – Jon Skeet May 21 '14 at 16:20
  • Not sure really, this is what my friend had setup when I joined the project and I started working from there. I'm not so familiar with what I'm doing here, but I think the idea is to use that cshtml code inside of the html code in some way – myselfesteem May 21 '14 at 16:24
  • Yeah, stop doing that :) The view should have very little code, IMO. – Jon Skeet May 21 '14 at 16:25
  • @JonSkeet I just talked to my friend and he said we're just trying to do a proof of concept. He knows its not ideal, but for my sake is trying to keep it simple :p. Do you know where I can at least fix this problem even though its not good practice to do what I'm doing? – myselfesteem May 21 '14 at 16:52
  • 1
    "Proof of concept" doesn't mean "do everything wrong." Putting all the code into the view isn't keeping things simple - it's making your life harder for yourself. Just stop doing it - it's *trivial* to create a new C# file. – Jon Skeet May 21 '14 at 17:09
  • @JonSkeet Yea, I agree with you and ended up doing just that. As to the error itself, turns out that if I removed the "this" in front of the type in the method signature, I don't get the error. Thanks much for your help! – myselfesteem May 30 '14 at 17:33
  • Well that will stop it from being an extension method - I assume there was a reason why the original code *wanted* it to be an extension method... – Jon Skeet May 30 '14 at 17:44

1 Answers1

0

If you were really smart you would encapsulate the design to take Delegates, reason being if you use a delegate you don't have to worry about referencing something static.

public delegate void MyUrlThing(string url, object optional = null);

Possibly some state...

public enum UrlState { None, Good, Bad }

Then void would become UrlState...

Also if you wanted you could also setup a text box and blindly give it CIL....

Then you would compile the delegates using something like this

http://www.codeproject.com/Articles/578116/Complete-Managed-Media-Aggregation-Part-III-Quantu

This way you can use also then optionally just use the IL to augment whatever you wanted.

You could also give it CSharp code I suppose...

If you want to keep you design you can also then optionally use interfaces... and then put the compiled dll in a directory and then load it etc... as traditionally

Jay
  • 3,276
  • 1
  • 28
  • 38
  • We're just trying to do a proof of concept so I'm not sure something so involved is needed at this point, but when we get down to implementing this I'll keep your thoughts in mind – myselfesteem May 21 '14 at 16:54
  • Thanks for the suggestion. Turns out that if I removed the "this" from every method signature, then the error went away. I also moved the c# code to a separate file. So thx for the help. I'll consider using these delegates when I'm doing the actual website – myselfesteem May 30 '14 at 17:35