且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用不同类型的对象填充列表

更新时间:2022-11-24 18:00:12

如果他们没有共同的基类,那么创建一个包装类是***的解决方案.同时,您可以更灵活地创建类似

If they doesn't have common base class then creating one wrapper class is the best solution. At the same time you can be more flexible and create something like

public class RecommendationItem
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string PageUrl { get; set; }
    public object Entity { get; set; }
}

因此您可以在此类中包含所有常见信息,并且客户端将无需检查他使用的对象类型.在这种情况下,再添加一种项目类型会更容易.在同一类型中,我添加了对实体本身的引用 - 如果需要对一种或两种项目类型进行某些特定处理,则可以使用它.

So you can include all common information in this class and client will not be required to check with which object type he works. In such case it would be easier to add one more item type. At the same type I added reference to entity itself - it can be used if some specific handling for one or two item types is required.