2011-02-25 65 views
0

我已经扩展一个类,像这样:扩展所提供的类

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.C ollections.Generic.List.List(System.Collections.Generic.IEnumerable)'有一些无效的参数。

参数1:无法从“System.Collections.ObjectModel.ReadOnlyCollection”到“System.Collections.Generic.IEnumerable”转换

可能有人请解释为什么会发生和最佳的解决方案?

+0

你的新旧代码对我来说看起来是一样的。 RegisteredDocks与CormantRadDock有什么关系?为什么它的类型改变了? – 2011-02-25 00:56:17

+0

我遍历了RegisteredDocks以记录每个码头的所有属性。我扩展RadDock以给它更多的属性。我也需要保存这些新的属性。因此,在迭代列表时,我可以将每个RadDock明确地转换为CormantRadDock来访问其附加属性,或者(我正在尝试做的)将RadDock的整个列表转换为CormantRadDock。 – 2011-02-25 01:00:00

回答

3

一些RegisteredDocks可能不是CormantRadDock,所以它们不能被添加到List<CormantRadDock>

如果你只在CormantRadDocks感兴趣,你可以按照类型过滤:

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

如果你肯定知道RegisteredDocks将只包含CormantRadDocks,你可以投每个项目:

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