簡體   English   中英

無法在第三方類上調用ToArray()

[英]Unable to call ToArray() on 3rd party class

我本來打算在3rd Party類(Kentico)上使用一些LINQ,但似乎無法這樣做,我也不知道為什么。 我的代碼本質上是:

using System;
using System.Collections.Generic;
using System.Linq;
// some additional namespaces

namespace test
{
   public partial class ProductFilter : CMSAbstractBaseFilterControl
   {
       protected IEnumerable<String> GetCategories(String parentName)
        {
            CategoryInfo info = CategoryInfoProvider.GetCategoryInfo(parentName, CMSContext.CurrentSite.SiteName);
            var children = CategoryInfoProvider.GetChildCategories(info.CategoryID, null, null, -1, null, CMSContext.CurrentSite.SiteID);

            children.ToArray();
        }
   }

此時我得到了錯誤

錯誤5'CMS.SettingsProvider.InfoDataSet'不包含'ToArray'的定義,並且找不到擴展方法'ToArray'接受類型為'CMS.SettingsProvider.InfoDataSet'的第一個參數(是否缺少using指令或組裝參考?)

CategoryInfoProvider.GetChildCategories定義為:

public static InfoDataSet<CategoryInfo> GetChildCategories(int categoryId, string where, string orderBy, int topN, string columns, int siteId);

InfoDataSet定義為:

public class InfoDataSet<InfoType> : ObjectDataSet<BaseInfo>, IEnumerable<InfoType>, IInfoDataSet, IEnumerable where InfoType : CMS.SettingsProvider.BaseInfo, new()
{
    public InfoDataSet();
    public InfoDataSet(DataSet sourceData);

    public InfoObjectCollection<InfoType> Items { get; protected set; }
    protected InfoType Object { get; set; }

    public InfoDataSet<InfoType> Clone();
    public IEnumerator<InfoType> GetEnumerator();
    public InfoType GetNewObject(DataRow dr);
    protected override ObjectCollection<BaseInfo> NewCollection();
}

看來接口已正確實現,我已經為LINQ包含了名稱空間,並且可以進行類似List<int> i; i.ToArray();調用List<int> i; i.ToArray(); List<int> i; i.ToArray(); 我錯過了拼圖的哪一部分?

嘗試忽略對AsEnumerable()的調用:

using System;
using System.Collections.Generic;
using System.Linq;
// some additional namespaces

namespace test
{
   public partial class ProductFilter : CMSAbstractBaseFilterControl
   {
       protected IEnumerable<String> GetCategories(String parentName)
        {
            CategoryInfo info = CategoryInfoProvider.GetCategoryInfo(parentName, CMSContext.CurrentSite.SiteName);
            var children = CategoryInfoProvider.GetChildCategories(info.CategoryID, null, null, -1, null, CMSContext.CurrentSite.SiteID);

            children.AsEnumerable().ToArray();
        }
   }
}

這個問題似乎是編譯器無法解決children作為一個IEnumerable,即使集合明確實現的接口。 強制將集合視為IEnumerable,就像AsEnumerable一樣,應允許ToArray正確解析。

暫無
暫無

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

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