将list <base />类转换为silverlight </derived>中的list <derived>类

时间:2013-03-26 09:27:48

标签: c# silverlight-4.0

我有一个基类(目录),它有以下参数:

string Name;
string Path;
List<Directory> SubDirectory;

现在我想创建一个新类,它将使用新属性扩展此类:

Bool Valid ;

所以我为派生类(DirectoryExt)编写以下代码:

Public class DirectoryExt : Directory

{
    Public bool Valid {get; set;}

    public DirectoryExt( Directory directory)

    {
       this.Name = directory.Name;
       this.Path = directory.Path;
       this.SubDirectory = directory.Subdirectory;
       this.Valid = false;
    } 

}

这里的问题是当我从现有的List<Directory> SubDirectory e)对象创建List<DirectoryExt> SubDirectory对象时,如何将此DirectoryExt(derived)转换为Directory(bas

有人可以建议解决方案吗?

2 个答案:

答案 0 :(得分:2)

使用LINQ to Objects:

this.SubDirectory = directory.SubDirectory.Select(s => new DirectoryExt(s)).ToList();

答案 1 :(得分:0)

this.SubDirectory = new ObservableCollection&lt;目录&gt;();

        if (directory.SubDirectory != null || directory.SubDirectory.Count != 0)
        {
            foreach (var item in directory.SubDirectory.Select(s => new DirectoryExt(s)).ToList())
            {
                this.SubDirectory.Add(item);
            }
        }

这是正确的解决方案......我已经测试过它并且有效。