Ainxty
Ainxty

Reputation: 63

List of different types

I have list of feed item objects and each feed item object has an Attributes property which in turn is also a list. However, this list needs to vary and I need to be able to later infer from the attributes what type of feed item I'm looking at.

I've tried several things so far, but I'm having a hard time wrapping my head around them as I do not entirely understand the more specific OOP concepts like generics. Here's what I have arrived at so far:

public class FeedItem
{
    public int Id { get; set; }
    public string Type { get; set; }
    public List<object> Attributes { get; set; }
    public DateTime Date { get; set; }
}

And here's the list:

var feedData = new List<FeedItem>();
feedData.Add(new FeedItem
        {
            Date = new DateTime(2015, 3, 3),
            Type = "library",
            Id = 1,
            Attributes = new List<Paper>() { 
             ... add papers here ... 
            }
        });

The problem I run into is that it's trying to convert Paper in object. The idea is to keep the Attributes property open to having a list of varied objects, as I do want to insert Papers, Authors, Publications, etc...

Thanks!

Upvotes: 0

Views: 585

Answers (2)

D Stanley
D Stanley

Reputation: 152521

A List<Paper> is not a List<object> - because it can only hold Papers. You can either declare it as a List<object> (or some other common base type to all of the types you want to support) and fill it with Papers:

feedData.Add(new FeedItem
        {
            Date = new DateTime(2015, 3, 3),
            Type = "library",
            Id = 1,
            Attributes = new List<object>() { 
             ... add papers here ... 
            }
        });

or make FeedItem generic - the drawback here is you must predefine what type of attributes the FeedItem can have:

public class FeedItem<T>
{
    public int Id { get; set; }
    public string Type { get; set; }
    public List<T> Attributes { get; set; }
    public DateTime Date { get; set; }
}

var feedData = new List<FeedItem<Paper>>();
feedData.Add(new FeedItem<Paper>
        {
            Date = new DateTime(2015, 3, 3),
            Type = "library",
            Id = 1,
            Attributes = new List<Paper>() { 
             ... add papers here ... 
            }
        });

Upvotes: 1

Ant P
Ant P

Reputation: 25221

Sounds like you need FeedItem to be generic:

public class FeedItem<T>
{
    public List<T> Attributes { get; set; }
}

var feedData = new List<FeedItem<Paper>>();
feedData.Add(new FeedItem<Paper>
    {
        Attributes = new List<Paper>() {

        }
    });

If you need multiple types of attributes in one list (but given that in your example you explicitly create a List<Paper>, I don't think you do), then have Paper, Author, etc. implement a common interface and type the List to that. They must all offer some common functionality - a list of a bunch of completely different things is useless, right?

Upvotes: 5

Related Questions