【问题标题】:Although static gives an error虽然静态给出了错误
【发布时间】:2018-02-06 14:44:45
【问题描述】:

我正在写一个关于日期时间的助手。

public static class DatepickerHelper
{
    public static MvcHtmlString Datepicker(this HtmlHelper htmlHelper, string name, object value = null, object htmlAttributes = null, EInputAddonPosition? addonPosition = EInputAddonPosition.Right, EInputGroupSize? groupSize = EInputGroupSize.Medium, EDateTimePickerFormat? Format = EDateTimePickerFormat.GunAyYil, bool? showRemoveButton = false, string onChangeFn = "");

    public static MvcHtmlString DatepickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null, EInputAddonPosition? addonPosition = EInputAddonPosition.Right, EInputGroupSize? groupSize = EInputGroupSize.Medium, EDateTimePickerFormat? Format = EDateTimePickerFormat.GunAyYil, bool? showRemoveButton = false, string onChangeFn = "");

    public static string GetStringValue(Enum value);
}

所有静态方法..看起来类似的错误,但我不明白

错误:

DatepickerHelper.DatepickerFor(HtmlHelper, Expression>, object, EInputAddonPosition?, EInputGroupSize?, EDateTimePickerFormat?, bool?, string)' 必须声明一个主体,因为它没有标记为抽象、外部或部分

【问题讨论】:

  • 错误信息很清楚。您创建了一些方法,但它们没有主体,并且因为它们没有标记为抽象,所以您会遇到错误。
  • 我不确定您是否理解static 的含义,您是否将abstractstatic 混淆了?
  • “静态”标记还不够吗? .我问是因为我不明白。我问是因为我不明白我必须做什么。 “错误信息很清楚。” > 是的,我明白了,但我不明白“错误信息很清楚。” > 是的,我明白了,但我不明白。

标签: asp.net-mvc html-helper mvchtmlstring


【解决方案1】:

静态方法需要方法体。

您当前的实现实际上什么都不做。

这将使您摆脱当前的错误,但请注意throw new NotImplementedException(); - 您需要实际实现该方法并返回适当的值。

public static class DatepickerHelper
{
    public static MvcHtmlString Datepicker(this HtmlHelper htmlHelper, string name, object value = null, object htmlAttributes = null, EInputAddonPosition? addonPosition = EInputAddonPosition.Right, EInputGroupSize? groupSize = EInputGroupSize.Medium, EDateTimePickerFormat? Format = EDateTimePickerFormat.GunAyYil, bool? showRemoveButton = false, string onChangeFn = "")
    {
        //notice there's a body to this static method now
        throw new NotImplementedException();
    }


    public static MvcHtmlString DatepickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null, EInputAddonPosition? addonPosition = EInputAddonPosition.Right, EInputGroupSize? groupSize = EInputGroupSize.Medium, EDateTimePickerFormat? Format = EDateTimePickerFormat.GunAyYil, bool? showRemoveButton = false, string onChangeFn = "")
    {
        //notice there's a body to this static method now
        throw new NotImplementedException();
    }

    public static string GetStringValue(Enum value)
    {
        //notice there's a body to this static method now
        throw new NotImplementedException();
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2014-01-06
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    相关资源
    最近更新 更多