C# DropDownList引起错误的扩展方法

C# DropDownList引起错误的扩展方法,c#,extension-methods,C#,Extension Methods,我有一个用于DropDownList的扩展方法,它抛出扩展方法只能在关键字this的非泛型、非嵌套静态类中声明。我不太清楚为什么,因为我在DropDownList public static class DropDownListExtensions { public static void populateDropDownList(this DropDownList source, Action delegateAction) { source.DataValueFiel

我有一个用于
DropDownList
的扩展方法,它抛出
扩展方法只能在关键字
this
的非泛型、非嵌套静态类中声明。我不太清楚为什么,因为我在
DropDownList

public static class DropDownListExtensions {
    public static void populateDropDownList(this DropDownList source, Action delegateAction) {
        source.DataValueField = "Key";
        source.DataTextField = "Value";
        source.DataSource = delegateAction;
        source.DataBind();
    }
}

可能是您的
dropdownstextensions
类声明为其他类的嵌套类。将其移动到名称空间级别。

可能是您的
DropDownListExtensions
类声明为其他类的嵌套类。将其移动到命名空间级别。

:)是的,就是这样。谢谢你@lazyberezovsky:)是的,就是这样。谢谢你,拉兹别列佐夫斯基