簡體   English   中英

IEnumerable在VB.net和C#中是否有所不同?

[英]Is IEnumerable different in VB.net and C#?

我正在將http://customfeedaggregator.codeplex.com/移植到c#,以使自己想到C#和WPF。

我陷入IEnumerable一個問題。

有一個課-blogpost.vb

'Represents a single blog post
Class BlogPost

    Private _title As String
    Private _datePublished As DateTime
    Private _url As Uri
    Private _category As String

    Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Property DatePublished() As DateTime
        Get
            Return _datePublished
        End Get
        Set(ByVal value As DateTime)
            _datePublished = value
        End Set
    End Property

    Property Url() As Uri
        Get
            Return _url
        End Get
        Set(ByVal value As Uri)
            _url = value
        End Set
    End Property

    Property Category() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property


End Class

以及一個共享功能,該功能可檢索供稿並將其轉換為博客文章。

Shared Function RetrieveFeeds(ByVal Address As String) As IEnumerable(Of BlogPost)

        Dim doc As XDocument = XDocument.Load(Address)

        Dim query = From item In doc...<item> _
        Let DataPubblicazione = CDate(item.<pubDate>.Value).ToLocalTime _
        Let TitoloPost = item.<title>.Value _
        Let Url = item.<link>.Value _
        Let Categoria = item.<category>.Value _
            Order By DataPubblicazione Descending _
            Select New BlogPost With _
            {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
             .Url = New Uri(Url), .Category = Categoria}

        Return query
    End Function

該類是一個標准,因此這不是問題。 但是RetreiveFeeds很難。

這是我的C#版本:

 public static IEnumerable<BlogPost> RetrieveFeeds(string Address)
    {
        XDocument doc = XDocument.Load(Address);

        var query = from item in doc.Descendants("item")
                    let DataPubblicazione = Convert.ToDateTime(item.Attribute("pubDate").Value)
                    let TitoloPost = item.Attribute("title").Value 
                    let Url = item.Attribute("link").Value 
                    let Categoria = item.Attribute("category").Value 
                    orderby DataPubblicazione descending 
                    select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  Url, Categoria};
        return query;
    }

在顯示選擇新博客文章的部分顯示的錯誤是:

無法使用集合初始化程序初始化類型“ FeedMe.BlogPost”,因為它未實現“ System.Collections.IEnumerable”。

因此,是否需要在數據類中顯式實現IEnumerable? 還是我的C#端口代碼錯誤? VB.net和C#之間有區別嗎?

實際上,正確的語法在C#和VB.NET之間非常相似:

原始C#:

select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  
                     Url, Categoria};

更正的C#:

select new BlogPost {DatePublished = DataPubblicazione , 
                     Title = EscapeXML(TitoloPost),  
                     Url = new Uri(Url), 
                     Category = Categoria};

原始的VB.NET:

Select New BlogPost With _
    {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
     .Url = New Uri(Url), .Category = Categoria}

使用對象初始化程序聲明new BlogPost ,需要命名參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM