【问题标题】:Unable to cast object of type 'System.IO.FileSystemInfo[]' to type 'System.Collections.Generic.IEnumerable`1[System.IO.DirectoryInfo]无法将“System.IO.FileSystemInfo[]”类型的对象转换为“System.Collections.Generic.IEnumerable`1[System.IO.DirectoryInfo]
【发布时间】:2016-02-10 21:24:55
【问题描述】:

我遇到了 PCL 中以下方法的转换错误问题,而在旧类库中我没有遇到任何问题。

    protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
    {
        FolderInfo folderInfo = new FolderInfo(folderPath);

        if (settings.MaxDepthLevel == 0 || level < settings.MaxDepthLevel)
        {
            try
            {
                DirectoryInfo currentDirectoryInfo = new DirectoryInfo(folderPath);

                foreach (DirectoryInfo directoryInfo in currentDirectoryInfo.GetDirectories())
                {
                    if (settings.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        continue;
                    }

                    FolderInfo subFolderInfo = GetFolderInfo(directoryInfo.FullName, level + 1);
                    folderInfo.Folders.Add(subFolderInfo);
                    subFolderInfo.Parent = folderInfo;
                }

                foreach (FileInfo fileInfo in currentDirectoryInfo.EnumerateFiles())
                {
                    if (settings.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        continue;
                    }

                    folderInfo.Files.Add(fileInfo);
                }

                folderInfo.Files.Sort((x, y) => x.Name.CompareTo(y.Name));
            }
            catch (UnauthorizedAccessException)
            {
            }
        }

        return folderInfo;
    }

我收到以下错误:

抛出异常:System.IO.FileSystem.dll 中的“System.InvalidCastException” 附加信息:无法将“System.IO.FileSystemInfo[]”类型的对象转换为“System.Collections.Generic.IEnumerable`1[System.IO.DirectoryInfo]”类型。

在 PCL 中,我在上面的“工作”代码中写出的行为有何不同?

感谢和最好的问候 迈克尔

【问题讨论】:

  • 更大的问题是为什么要使用 System.IO.FileSystem.dll。您必须记录您测试的目标。它现在在 CoreFx 中看起来肯定是坏了,一个相当简单的铸造错误。向项目提交错误。
  • 谢谢我通过VS2015提交了一个问题。

标签: c#


【解决方案1】:

Windows Phone 应用程序不使用操作系统的文件系统 系统并且仅限于使用隔离存储来持久化和 访问文件,所以这个命名空间不提供任何额外的 功能。

http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.io%28v=vs.105%29.aspx

你也应该看看这个SO answer.

【讨论】:

  • 谢谢 - 不幸的是我也不能使用 PCLStorage:PCLStorage 1.0.2 与 DNXCore,Version=v5.0 (win7-x86) 不兼容。 Microsoft.Bcl.Async 1.0.165 与 DNXCore 不兼容,版本=v5.0 (win7-x86)。 Microsoft.Bcl 1.1.6 与 DNXCore,Version=v5.0 (win7-x86) 不兼容。部分软件包与 DNXCore 不兼容,Version=v5.0 (win7-x86)。
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多