我决定帮助我的朋友做一个他正在做的项目。我正试着为他写一个测试网页来验证一些新的功能,但是在我的自动生成的代码中我得到了
CS1106: Extension method must be defined in a non-generic static class用index.cshtml实现代码并不是最好的方法,但我们只是尝试做一个概念证明,稍后会做一个适当的实现。
在我看过的所有地方,他们几乎都说我定义的所有函数都必须在静态类中(如错误状态所示)。这并不是很糟糕,除了包含我所有函数的类是自动生成的,而不是静态的。我真的不确定我可以更改哪些设置来修复这个问题。
下面是相关(我相信)部分代码的副本。部分或全部功能的实现可能不正确。我还没测试过呢
@{
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>发布于 2014-05-22 00:25:35
如果你真的很聪明,你应该封装这个设计来接受委托,原因是如果你使用委托,你就不必担心引用静态的东西。
public delegate void MyUrlThing(string url, object optional = null);
可能是某种状态...
public enum UrlState { None, Good, Bad }
然后void会变成UrlState..。
此外,如果你愿意,你也可以设置一个文本框并盲目地给它CIL……
然后,您将使用如下所示的内容编译委托
http://www.codeproject.com/Articles/578116/Complete-Managed-Media-Aggregation-Part-III-Quantu
这样,您也可以使用,然后可以选择使用IL来增强您想要的任何内容。
我想你也可以给它提供CSharp代码...
如果你想保持你的设计,你也可以选择使用接口...然后将编译后的dll放在一个目录中,然后加载它,等等。一如既往
https://stackoverflow.com/questions/23788746
复制相似问题