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

时间:2016-06-22 09:03:29

标签: c# .net winforms

在form1中我添加了两个类

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是一个嵌套类

1 个答案:

答案 0 :(得分:1)

C#语言不允许您在嵌套类中定义扩展方法,请参阅Why are extension methods only allowed in non-nested, non-generic static class?

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

导致此错误的结构

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);
    }
}

结构防止此错误

{{1}}