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

C# 扩展方法必须在非泛型静态类中定义,c#,asp.net,compiler-errors,C#,Asp.net,Compiler Errors,我有个问题。 重命名此Web表单后,出现此错误,但我将所有内容更改为新名称 但我得到了这个错误。 请帮忙。 代码: 使用系统; 使用System.Collections.Generic; 使用系统数据; 利用制度全球化; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 公共部分类bVoteAnswer:System.Web.UI.Page { 用户投票ue=新的用户投票(); bVotes bv=

我有个问题。 重命名此Web表单后,出现此错误,但我将所有内容更改为新名称 但我得到了这个错误。 请帮忙。 代码:

使用系统;
使用System.Collections.Generic;
使用系统数据;
利用制度全球化;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类bVoteAnswer:System.Web.UI.Page
{
用户投票ue=新的用户投票();
bVotes bv=新的bVotes();
bVoteAnswers ve=新的bVoteAnswers();
公共无效页面加载(对象发送方,事件参数e)
{
dropdownlist.Enabled=false;
int BuildingId=Convert.ToInt32(会话[“BuildingId]”);
DataTable dt=新的DataTable();
dt=bv。选择(0,“”,“”,“”,BuildingId,“”,0);
Grid_Vote.DataSource=dt;
Grid_Vote.DataBind();
}
已创建受保护的无效网格(对象发送方,GridViewRowEventArgs e)
{
e、 行。单元格[0]。可见=假;
}
受保护的无效网格\u投票\u选择的索引已更改(对象发送方,事件参数e)
{
dropdownlist.Enabled=true;
int-VoteId=Convert.ToInt32(Grid\u-Vote.SelectedRow.Cells[0].Text);
DataTable dt=新的DataTable();
dt=ve。选择(0,VoteId,“”);

对于(inti=0;i扩展方法需要在静态类中

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

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

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

}

扩展方法必须在静态类中

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

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

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

}

扩展方法
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));
    }
}
公共部分类bVoteAnswer:System.Web.UI.Page
{
用户投票ue=新的用户投票();
bVotes bv=新的bVotes();
bVoteAnswers ve=新的bVoteAnswers();
公共无效页面加载(对象发送方,事件参数e)
{
dropdownlist.Enabled=false;
int BuildingId=Convert.ToInt32(会话[“BuildingId]”);
DataTable dt=新的DataTable();
dt=bv。选择(0,“”,“”,“”,BuildingId,“”,0);
Grid_Vote.DataSource=dt;
Grid_Vote.DataBind();
}
已创建受保护的无效网格(对象发送方,GridViewRowEventArgs e)
{
e、 行。单元格[0]。可见=假;
}
受保护的无效网格\u投票\u选择的索引已更改(对象发送方,事件参数e)
{
dropdownlist.Enabled=true;
int-VoteId=Convert.ToInt32(Grid\u-Vote.SelectedRow.Cells[0].Text);
DataTable dt=新的DataTable();
dt=ve。选择(0,VoteId,“”);

对于(int i=0;i扩展方法
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));
    }
}
公共部分类bVoteAnswer:System.Web.UI.Page
{
用户投票ue=新的用户投票();
bVotes bv=新的bVotes();
bVoteAnswers ve=新的bVoteAnswers();
公共无效页面加载(对象发送方,事件参数e)
{
dropdownlist.Enabled=false;
int BuildingId=Convert.ToInt32(会话[“BuildingId]”);
DataTable dt=新的DataTable();
dt=bv。选择(0,“”,“”,“”,BuildingId,“”,0);
Grid_Vote.DataSource=dt;
Grid_Vote.DataBind();
}
已创建受保护的无效网格(对象发送方,GridViewRowEventArgs e)
{
e、 行。单元格[0]。可见=假;
}
受保护的无效网格\u投票\u选择的索引已更改(对象发送方,事件参数e)
{
dropdownlist.Enabled=true;
int-VoteId=Convert.ToInt32(Grid\u-Vote.SelectedRow.Cells[0].Text);
DataTable dt=新的DataTable();
dt=ve。选择(0,VoteId,“”);

对于(int i=0;i从

GetPersianDate(this DateTime date)
无论如何,您并没有将其用作扩展方法,这将使您能够编写

DateTime.Now.GetPersianDate()
您可以在此处阅读有关扩展方法的更多信息:

从中删除“this”

GetPersianDate(this DateTime date)
无论如何,您并没有将其用作扩展方法,这将使您能够编写

DateTime.Now.GetPersianDate()
您可以在此处阅读有关扩展方法的更多信息:

当怀疑是你自己还是编译器出错时,通常是你自己出错。当怀疑是你自己还是编译器出错时,通常是你自己出错。