使用ToString构建文件路径列表

时间:2015-07-24 21:06:12

标签: c# list unity3d tostring

我已经创建了一个构造函数类来调用来构建我的列表,但是我收到了一个错误:

  

错误CS0029:无法隐式转换类型System.Collections.Generic.List<string>' to System.Collections.Generic.List'

我很困惑,因为我认为文件列表是一个字符串。我需要将类型转换为?

// Create Constructor Class
public class ImagePath
{
    public string path;

    public ImagePath (string newPath)
    {
        path = newPath;
    }
}

public class LoadGalleryList : MonoBehaviour
{       
    void Start()
    {
        // Call ImagePath Constructor Class to build Lists
        List<ImagePath> arctopithecusImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> arctopithecusPNGImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> gulonImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> scythianWolfImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> simivulpaImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> succorathImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
        List<ImagePath> tatusImageList = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg").ToList();
    }
}

1 个答案:

答案 0 :(得分:4)

GetFiles().ToList()会返回List<string>,您要将其存储在List<ImagePath>对象中。您需要将List<string>中的每个元素转换为ImagePath

LINQ可以解决您的问题:

List<ImagePath> imagePathList =
    Directory.GetFiles(directoryPath).Select(f => new ImagePath(f)).ToList();