0

我正在尝试设置和构建https://resources.samsungdevelopers.com/Gear_VR/030_Create_VR_App_or_Game_Using_a_Game_Engine/Exercise_2%3A_Create_a_Splash_Screen/Step_7%3A_Build_and_Run_the_Application

当我尝试构建应用程序时,它会引发以下错误: Assets/Workshop/Scr​​ipts/TextureLoader.cs(114,31): error CS1105: `TextureLoader.GetFilesWithExtensions(this DirectoryInfo, params string[])': Extension methods must be声明为静态

// Gets the list of files in the directory by extention and filters for our specified ones (images)
public IEnumerable<FileInfo> GetFilesWithExtensions (this DirectoryInfo dir, params string[] extensions) {
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}

public bool isFinishedLoadingFirstTexture() {
    return mFinishedFirstTexture;
}

我已经将其更改为标题中的公共静态类。

4

3 回答 3

0

静态类对方法没有多大作用。您需要使您的方法静态才能使扩展工作!

public static IEnumerable<FileInfo> GetFilesWithExtensions (this DirectoryInfo dir, params string[] extensions) {
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}

如果您需要另一个示例,我在这里写了一个扩展方法!

于 2017-02-23T12:12:04.077 回答
0

我为了做一个扩展方法,类必须是publicand static

功能必须是publicstatic太。你错过了这部分。

public static class TextureLoader
{
    public static IEnumerable<FileInfo> GetFilesWithExtensions(this DirectoryInfo dir, params string[] extensions)
    {
        IEnumerable<FileInfo> files = dir.GetFiles();
        return files.Where(f => extensions.Contains(f.Extension));
    }
}
于 2017-02-23T12:12:26.660 回答
0

这样修改你的代码,错误就会消失

private IEnumerable<FileInfo> GetFilesWithExtensions(DirectoryInfo dir, string[] extensions)
{
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}
于 2017-05-24T16:24:52.993 回答