C# 为什么必须在顶级静态类中定义获取错误扩展方法?

C# 为什么必须在顶级静态类中定义获取错误扩展方法?,c#,.net,winforms,C#,.net,Winforms,在表格1中,我添加了两个类 public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) return this; return null; } private const

在表格1中,我添加了两个类

public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter)) return this;
        return null;
    }

    private const string fileSizeFormat = "fs";
    private const Decimal OneKiloByte = 1024M;
    private const Decimal OneMegaByte = OneKiloByte * 1024M;
    private const Decimal OneGigaByte = OneMegaByte * 1024M;

    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        if (format == null || !format.StartsWith(fileSizeFormat))
        {
            return defaultFormat(format, arg, formatProvider);
        }

        if (arg is string)
        {
            return defaultFormat(format, arg, formatProvider);
        }

        Decimal size;

        try
        {
            size = Convert.ToDecimal(arg);
        }
        catch (InvalidCastException)
        {
            return defaultFormat(format, arg, formatProvider);
        }

        string suffix;
        if (size > OneGigaByte)
        {
            size /= OneGigaByte;
            suffix = "GB";
        }
        else if (size > OneMegaByte)
        {
            size /= OneMegaByte;
            suffix = "MB";
        }
        else if (size > OneKiloByte)
        {
            size /= OneKiloByte;
            suffix = "kB";
        }
        else
        {
            suffix = " B";
        }

        string precision = format.Substring(2);
        if (String.IsNullOrEmpty(precision)) precision = "2";
        return String.Format("{0:N" + precision + "}{1}", size, suffix);

    }

    private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
    {
        IFormattable formattableArg = arg as IFormattable;
        if (formattableArg != null)
        {
            return formattableArg.ToString(format, formatProvider);
        }
        return arg.ToString();
    }

}

public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
    }
}
然后我就这样使用它:

FileInfo fi = new FileInfo(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
label17.Text = ExtensionMethods.ToFileSize(fi.Length);
但是在ToFilesize上的ExtensionMethods类上获取错误:

错误1扩展方法必须在顶级静态类中定义; ExtensionMethods是一个嵌套类

C语言不允许从嵌套类中定义扩展方法,请参阅

根据评论中的讨论,应通过以下方法解决问题:

导致此错误的结构

public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }

    // class is in SomeClass, therefore it is a nested class. You cannot define extension mathods here
    public static class ExtensionMethods
    {
        public static string ToFileSize(this long l)
        {
            return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
        }
    }
}
public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }           
}


// now you can, because it is not nested inside some other class
public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        // note the SomeTopClass before FileSizeFormatProvider!!
        return String.Format(new SomeTopClass.FileSizeFormatProvider(), "{0:fs}", l);
    }
}
防止此错误的结构

public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }

    // class is in SomeClass, therefore it is a nested class. You cannot define extension mathods here
    public static class ExtensionMethods
    {
        public static string ToFileSize(this long l)
        {
            return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
        }
    }
}
public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }           
}


// now you can, because it is not nested inside some other class
public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        // note the SomeTopClass before FileSizeFormatProvider!!
        return String.Format(new SomeTopClass.FileSizeFormatProvider(), "{0:fs}", l);
    }
}

查看此答案:您在此处发布的代码与错误消息不匹配。你能给我们看一下代码所在的整个CS文件吗?@nozzleman是的,但它不直接在FileSizeFormatProvider类的上面。之前有一些方法。你需要正确定义扩展方法(在顶级静态类中)。或者删除“this”关键字,以当前方式将其用作常规帮助器方法。