我使用扩展方法来扩展 html 助手来制作 RSS 转发器:
public static string RSSRepeater(this HtmlHelper html, IEnumerable<IRSSable> rss)
{
string result="";
foreach (IRSSable item in rss)
{
result += "<item>" + item.GetRSSItem().InnerXml + "</item>";
}
return result;
}
因此,我让我的业务对象之一实现 IRSSable,并尝试将其传递给 HTML 帮助程序。但我似乎无法让它发挥作用,我已经尝试过:
<%=Html.RSSRepeater(ViewData.Model.GetIssues(null, null, "") as IEnumerable<IRSSable>) %>
编译正常,但传递了 null
<%=Html.RSSRepeater(ViewData.Model.GetIssues(null, null, "")) %>
Intellisense 抱怨无法将 IEnumerable 问题传递给 Enumerable IRAScible
- 你是怎么做到的?我调用的方法肯定会返回
IEnumberable<Issue>
并且问题明确实现了 IRSSAble
啊...尝试:
public static string RSSRepeater<T>(this HtmlHelper html, IEnumerable<T> rss)
where T : IRSSable
{
...
}
然后,这应该允许您传递实现的任何事物序列IRSSable
- 泛型类型推断应该意味着您不需要指定T
(as Issue
)你自己 - 编译器会处理它。
顺便一提 -避免在这里串联 http://www.yoda.arachsys.com/csharp/stringbuilder.html; StringBuilder
最好是:
StringBuilder result = new StringBuilder();
foreach (IRSSable item in rss)
{
result.Append("<item>").Append(item.GetRSSItem().InnerXml).Append("</item>");
}
return result.ToString();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)