【发布时间】:2015-02-02 15:02:12
【问题描述】:
我目前正在开发一项 REST 服务,该服务在我的 CMS(Orchard)中创建页面和链接,这两者都需要放在我的菜单中。
这是我目前的解决方案:
public class ConfigurationDTO
{
public ShellSettings Tenant { get; set; }
public SetupContext SetupContext { get; set; }
public IList<IList<Page>> Pages { get; set; }
public IList<IList<Link>> Links { get; set; }
public ConfigurationDTO() { }
}
我的页面.cs
public enum PageType
{
HomePage,
DownloadPage,
StandardPage
}
public class Page
{
public PageType Type { get; set; }
public string MenuText { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Culture { get; set; }
public bool Publish { get; set; }
public Page() { }
}
链接.cs
public class Link
{
public string Url { get; set; }
public string MenuText { get; set; }
public string Culture { get; set; }
}
我有两个包含其他列表的列表。内部列表包含本地化项目(同一内容项目的英语、德语和法语版本)。
我的大部分“元”代码对于链接和页面都是相同的,这就是我重构代码的原因。
这是我的重构尝试(不工作):
public interface IItem
public class Page : IItem
public class Link : IItem
我的新列表
public IList<IList<IItem>> Items{ get; set; }
我的目标:
foreach (var item in config.Items)
{
foreach (var translation in item)
{
var page = translation as Page;
if (page != null) doSomething();
var link = translation as Link;
if (link != null) doSomethingElse();
}
}
有没有办法从这个列表中获取特定的实现(意思是如果它是一个页面,则创建一个新的页面条目,反之亦然)?
编辑:我用相同的原理编写了一个小示例程序,它可以工作,这意味着我的问题出在其他地方。
你可以说这是我在 stackoverflow 上的第一篇文章,因为我只是告诉你什么不起作用,而不是实际问题是什么。我的问题是我的主菜单中的顺序是错误的:
我想要什么: 第 1 页 |第2页|链接1 |第 3 页
我得到了什么: 第 1 页 |第2页|第 3 页 |链接1
我的代码创建所有页面,然后创建链接并将它们添加到菜单中。解决方法 -> 每个项目都有一个 order 属性。 我将来可能会更改它,以便我只需要一个列表,但现在我的解决方法就足够了。
谢谢大家。
【问题讨论】:
-
doSomething和doSomethingElse是什么?你确定你不能通过多态来实现这一点(即不同的实现,例如IItem.Create(someClient))?
标签: c# list generics interface