C# 非静态字段、方法或属性需要对象引用的解决方案

C# 非静态字段、方法或属性需要对象引用的解决方案,c#,asp.net,C#,Asp.net,我得到(编译器错误)“错误CS0120非静态字段、方法或属性需要对象引用”。下面提到了我的代码。请任何人帮我解决这个问题 private static Task<string> SendPasswordResetVerificationCode(string email) { string randomVerificationCode = new Random().Next(1000, 9999).ToString(CultureInfo.CurrentCu

我得到(编译器错误)“错误CS0120非静态字段、方法或属性需要对象引用”。下面提到了我的代码。请任何人帮我解决这个问题

 private static Task<string> SendPasswordResetVerificationCode(string email)
    {
        string randomVerificationCode = new Random().Next(1000, 9999).ToString(CultureInfo.CurrentCulture);

        ////send verification code to given email
        if (email != null)
        {
            SendEmail(email, randomVerificationCode);
            return Task.FromResult("VerificationCode Sent");
        }

        return Task.FromResult("VerificationCode Sent");
    }

    /// <summary>
    /// Sends an email for verification code
    /// </summary>
    /// <param name="email">email address that will receive verification code</param>
    /// <param name="verificationCode">verification code</param>
    private void SendEmail(string email, string verificationCode)
    {
        Task.Run(() =>
        {
            try
            {
                    MailMessage mail = new MailMessage();
                    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
                    mail.From = new MailAddress("v@gmail.com");
                    mail.To.Add(email);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Verification Code";
                     mail.Body = verificationCode;
                    smtpServer.Port = 587;

                    smtpServer.UseDefaultCredentials = false;
                    smtpServer.EnableSsl = true;
                    smtpServer.Send(mail);
                    mail.Dispose();
            }
            catch (Exception ex)
            {
                Utilities.Logging.LogManager.Write(ex);
            }
        });
    }
私有静态任务SendPasswordResetVerificationCode(字符串电子邮件)
{
string randomVerificationCode=new Random().Next(10009999).ToString(CultureInfo.CurrentCulture);
////将验证码发送到给定的电子邮件
如果(电子邮件!=null)
{
发送电子邮件(电子邮件、随机验证代码);
返回Task.FromResult(“已发送验证代码”);
}
返回Task.FromResult(“已发送验证代码”);
}
/// 
///发送验证代码的电子邮件
/// 
///将接收验证代码的电子邮件地址
///验证码
私有void sendmail(字符串电子邮件、字符串验证代码)
{
Task.Run(()=>
{
尝试
{
MailMessage mail=新的MailMessage();
SmtpClient smtpServer=新的SmtpClient(“smtp.gmail.com”);
mail.From=新邮件地址(“v@gmail.com");
mail.To.Add(电子邮件);
mail.IsBodyHtml=true;
mail.Subject=“验证码”;
mail.Body=验证代码;
smtpServer.Port=587;
smtpServer.UseDefaultCredentials=false;
smtpServer.EnableSsl=true;
发送(邮件);
mail.Dispose();
}
捕获(例外情况除外)
{
实用程序.Logging.LogManager.Write(ex);
}
});
}

看看您已有的两种方法,即
SendPasswordResetVerificationCode
SendEmail
,其中SendEmail是实例方法,第一种是静态方法。现在查看错误消息。很清楚,当您从静态方法调用
sendmail
时,非静态字段、方法或属性需要
对象引用

因此,作为一种静态方法,解决方案是快速而简单的更改
sendmail
。因此,定义如下:

private static void SendEmail(string email, string verificationCode)
{    
    // code here
}

注意:如果您没有任何特定原因将
SendPasswordResetVerificationCode
设置为静态,则意味着您也可以从签名中删除静态,而无需更改
sendmail的签名
,但实际调用应使用引用对象

您需要发布更多信息。。。哪一行给了你错误?你的方法必须是静态的吗?尝试删除静态访问修饰符。在哪里遇到异常?请检查您看到的异常并提供其他信息。这是编译器错误,对吗?顺便说一句,根据您的条件逻辑,您应该返回Task.FromResult(“VerificationCodeSent”);就在你的if声明之外一次