asp.netvb.netinheritancesyndicationfeed

Extending SyndicationItem in VB.net


I need your help for an issue about inheritance. In a project of mine I am using a the SyndicationFeed .net class to read several feed a make a ul of its elements. For every element I want to show the feed's image as well, so I wanted to assign the same ImageUrl property of the feed to the single item. So I started by creating a derived class:

Public Class SyndicationItemWImage
 Inherits SyndicationItem
 Private mItemImage As Uri

 Public Property ItemImage As Uri
  Get
   Return mItemImage
  End Get
  Set(value As Uri)
   mItemImage = value
  End Set
  End Property
End Class

Then I would initialize the object and populate it

Dim BlogsPostsWImage As List(Of SyndicationItemWImage)
BlogsPostsWImage = New List(Of SyndicationItemWImage)
…
[initialize SyndFeed]
…
BlogsPostsWImage.AddRange(SyndFeed.Items.ToList.GetRange(0, 10))

Where SynFeed is a well working SyndicationFeed object. Unfortunately I get an error that the cast is invalid:

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List1[System.ServiceModel.Syndication.SyndicationItem]' to type 'System.Collections.Generic.IEnumerable1[lucamauricom.SyndicationItemWImage]'. at lucamauricom._default.Page_Load(Object sender, EventArgs e)

I do not understand why: shouldn't the cast from a parent class to a child one be allowed? I think I am missing something fundamental here… not sure what.

Thanks for any help you can provide.


Solution

  • Unfortunately nobody appears to be able to help me with this, so I went on attacking the problem from another angle: I now have an alternative method to accomplish the same goal. I am answering my own question here to document it.

    Starting from the same SynFeed as above, I filled a temporary List and then I added an ElementExtension:

    Dim TempItems As New List(Of SyndicationItem)
    
    If SyndFeed.Items.Count >= CurrentFeed.TotalElements Then
      TempItems.AddRange(SyndFeed.Items.ToList.GetRange(0, CurrentFeed.TotalElements))
    Else
      TempItems.AddRange(SyndFeed.Items.ToList)
     End If
    
     For Each CurrItem As SyndicationItem In TempItems
       CurrItem.ElementExtensions.Add("favico", "", SingleFeed.Descendants("//bits.wikimedia.org/favicon/wikipedia.ico")
     Next
    
     FeedItems.AddRange(TempItems)
    

    The ElementExtension is called favico, it contains no namespace and has value //bits.wikimedia.org/favicon/wikipedia.ico. This way, I added the data as a sort of "custom field" that can be easily read on the ASPX page like this:

    <img src='<%#CType(Container.DataItem, SyndicationItem).ElementExtensions.ReadElementExtensions(Of String)("favico", "").Item(0).ToString%>' height="16" />
    

    I still cannot figure out what is wrong with the derived class of my original idea, but this second way of dealing with the issue is probably better within the context of Syndication class.