扩展提供的类

时间:2011-02-25 00:46:09

标签: c#

我已经像这样扩展了一个类:

public class CormantRadDock : Telerik.Web.UI.RadDock
{
    public enum Charts { LineChart, PieChart, BarChart };

    public Charts ChartType { get; set; }
    public bool LegendEnabled { get; set; }
    public string ChartName { get; set; }

    public CormantRadDock() : base()
    {
    }
}

我现在正尝试在其他位置调整一些代码以适应此更新。

旧代码是这样的:

List<RadDock> docks = new List<RadDock>(dockLayout.RegisteredDocks);

其中RegisteredDocks的类型为"System.Collections.ObjectModel.ReadOnlyCollection<RadDock>"

我不明白为什么这是不可能的:

List<CormantRadDock> docks = new List<CormantRadDock>(dockLayout.RegisteredDocks);

我收到错误:

'System.Collections.Generic.List.List(System.Collections.Generic.IEnumerable)'的最佳重载方法匹配有一些无效的参数。

参数1:无法从'System.Collections.ObjectModel.ReadOnlyCollection'转换为'System.Collections.Generic.IEnumerable'

有人可以解释为什么会发生这种情况并提供最佳解决方案吗?

1 个答案:

答案 0 :(得分:3)

部分RegisteredDocks可能不是CormantRadDock,因此无法将其添加到List<CormantRadDock>

如果您只对CormantRadDocks感兴趣,可以按类型过滤:

List<CormantRadDock> docks = dockLayout.RegisteredDocks.OfType<CormantRadDock>().ToList();

如果您确定RegisteredDocks只包含CormantRadDocks,则可以投射每个项目:

List<CormantRadDock> docks = dockLayout.RegisteredDocks.Cast<CormantRadDock>().ToList();
相关问题