必须在非泛型静态类中定义扩展方法

时间:2017-07-13 07:54:33

标签: c# asp.net compiler-errors

我遇到了问题。 重命名此Web表单后,我收到此错误但我将所有内容更改为新名称 但是我得到了这个错误。 Plz,帮助。 代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class bVoteAnswer : System.Web.UI.Page
{
    UserVotes ue = new UserVotes();
    bVotes bv = new bVotes();
    bVoteAnswers ve = new bVoteAnswers();
    public void Page_Load(object sender, EventArgs e)
    {

        dropdownlist.Enabled = false;
        int BuildingId = Convert.ToInt32(Session["BuildingId"]);
        DataTable dt = new DataTable();
        dt = bv.Select(0, "", "", "", BuildingId, "",0);
        Grid_Vote.DataSource = dt;
        Grid_Vote.DataBind();

    }
protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false;
}

protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e)
{
    dropdownlist.Enabled = true;
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
    DataTable dt = new DataTable();
    dt = ve.Select(0,VoteId,"");
    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString());
        dropdownlist.Items.Add(l);
    }
}

protected void btn_insert_Click(object sender, EventArgs e)
{
    int OwnerId = Convert.ToInt32(Session["OwnerId"]);
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
    DataTable dt = new DataTable();
    dt = ve.Select(0, VoteId, dropdownlist.SelectedValue);
    int AnswerId = Convert.ToInt32(dt.Rows[0][0]);
    DateTime ClientDateTime = DateTime.Now;
    string PersianDate = GetPersianDate(ClientDateTime);
    ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate);
}


public static string GetPersianDate(this DateTime date)
{
    PersianCalendar jc = new PersianCalendar();
    return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));
}

}

我将名称更改为名字但错误显示 我认为这不是一个错误,Visual Studio不是上帝

3 个答案:

答案 0 :(得分:1)

扩展方法需要在静态类中。

public partial class bVoteAnswer

这不是静态类。 将函数移动到静态类。

public static class ExtensionMethods
{
   public static string GetPersianDate(this DateTime date)
    {
        PersianCalendar jc = new PersianCalendar();
        return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), 
               jc.GetMonth(date), jc.GetDayOfMonth(date));
      }

}

答案 1 :(得分:1)

扩展方法GetPersianDate需要在静态类中定义。你可以像这样重构:

public partial class bVoteAnswer : System.Web.UI.Page
{
    UserVotes ue = new UserVotes();
    bVotes bv = new bVotes();
    bVoteAnswers ve = new bVoteAnswers();
    public void Page_Load(object sender, EventArgs e)
    {

        dropdownlist.Enabled = false;
        int BuildingId = Convert.ToInt32(Session["BuildingId"]);
        DataTable dt = new DataTable();
        dt = bv.Select(0, "", "", "", BuildingId, "",0);
        Grid_Vote.DataSource = dt;
        Grid_Vote.DataBind();

    }

    protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
    }

    protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e)
    {
        dropdownlist.Enabled = true;
        int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
        DataTable dt = new DataTable();
        dt = ve.Select(0,VoteId,"");
        for (int i = 0; i <= dt.Rows.Count; i++)
        {
            ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString());
            dropdownlist.Items.Add(l);
        }
    }

    protected void btn_insert_Click(object sender, EventArgs e)
    {
        int OwnerId = Convert.ToInt32(Session["OwnerId"]);
        int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
        DataTable dt = new DataTable();
        dt = ve.Select(0, VoteId, dropdownlist.SelectedValue);
        int AnswerId = Convert.ToInt32(dt.Rows[0][0]);
        DateTime ClientDateTime = DateTime.Now;
        string PersianDate = GetPersianDate(ClientDateTime);
        ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate);
    }
}

public static class PersionCalendarExtension
{
    public static string GetPersianDate(this DateTime date)
    {
        PersianCalendar jc = new PersianCalendar();
        return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));
    }
}

答案 2 :(得分:0)

中删除'this'
GetPersianDate(this DateTime date)

无论如何你都没有将它用作扩展方法,这使你能够编写例如

DateTime.Now.GetPersianDate()

您可以在此处详细了解扩展方法: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods