C# I';我在get方法中遇到堆栈溢出异常
当我重构代码时,当我的视图试图访问我的类的成员时,我的get方法抛出了System.StackOverflowException。 这是我第一次使用??(空合并运算符)在c#中 那是我的型号:C# I';我在get方法中遇到堆栈溢出异常,c#,asp.net-mvc,C#,Asp.net Mvc,当我重构代码时,当我的视图试图访问我的类的成员时,我的get方法抛出了System.StackOverflowException。 这是我第一次使用??(空合并运算符)在c#中 那是我的型号: using System; using System.ComponentModel.DataAnnotations; namespace Extranet45.Models { public class Complaint { public string Nome
using System;
using System.ComponentModel.DataAnnotations;
namespace Extranet45.Models
{
public class Complaint
{
public string Nome
{
get => Nome ?? "Nome não informado.";
private set => Nome = value ?? "Nome não informado.";
}
public string Email
{
get => Email ?? "Email não informado.";
private set => Email = value ?? "Email não informado.";
}
[Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
public string Denuncia
{
get => Denuncia ?? "O texto da denuncia não foi encontrado.";
private set => Denuncia = value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
}
public Complaint() { }
public Complaint(string nome, string email, string denuncia)
{
Nome = nome;
Email = email;
Denuncia = denuncia;
}
}
}
using System;
using System.Web.Mvc;
using Extranet45.Models;
using Utils;
namespace Extranet45.Controllers
{
public class ComplaintController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Send");
}
// GET: Complaint/Send
public ActionResult Send()
{
return View(new Complaint());
}
}
}
这是我的控制器/操作:
using System;
using System.ComponentModel.DataAnnotations;
namespace Extranet45.Models
{
public class Complaint
{
public string Nome
{
get => Nome ?? "Nome não informado.";
private set => Nome = value ?? "Nome não informado.";
}
public string Email
{
get => Email ?? "Email não informado.";
private set => Email = value ?? "Email não informado.";
}
[Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
public string Denuncia
{
get => Denuncia ?? "O texto da denuncia não foi encontrado.";
private set => Denuncia = value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
}
public Complaint() { }
public Complaint(string nome, string email, string denuncia)
{
Nome = nome;
Email = email;
Denuncia = denuncia;
}
}
}
using System;
using System.Web.Mvc;
using Extranet45.Models;
using Utils;
namespace Extranet45.Controllers
{
public class ComplaintController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Send");
}
// GET: Complaint/Send
public ActionResult Send()
{
return View(new Complaint());
}
}
}
这是我的视图中引发异常的部分
@model Extranet45.Models.Complaint
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Denúncia";
ViewBag.ShowHeader = false;
ViewBag.BodyClass = "profile-page"; //Faz com que o header fique com a altura menor, porém ñão é uma solução elegante, refatorar
ViewBag.HeaderImageUrl = "https://github.com/creativetimofficial/material-kit/blob/master/assets/img/city-profile.jpg?raw=true";
}
@using (Html.BeginForm("Send", "Complaint", FormMethod.Post, new { @Class = "contact-form"}))
{
<div class="section section-contacts">
<div class="col-md-8 ml-auto mr-auto">
@ViewBag.ErrorMessage
<h2 id="main-title" class="text-center title">Denuncie</h2>
<h4 class="text-center description">Caso você tenha alguma reclamação ou precise comunicar algo incorreto ao Ceape, nos envie uma mensagem abaixo ou um email para <a href="mailto:denuncias@ceapema.org.br">denuncias@@ceapema.org.br</a></h4>
<div class="row justify-content-center">
<div class="col-md-8">
<div class=" form-group bmd-form-group">
<label for="textbox-nome" class="bmd-label-static">Nome - Opcional</label>
@Html.TextBoxFor(m => m.Nome, new { @id = "textbox-nome", @Type = "Text", @Class = "form-control", name = "nome" })
</div>
</div>
</div>
我不知道为什么我使用null合并运算符会导致堆栈溢出。这是因为您的Nome、Email和Denucia设置程序在一个无限过程中调用它们自己。可以通过为每个属性使用支持字段来解决此问题
private string _nome ;
public string Nome
{
get => _nome ?? "Nome não informado.";
private set => _nome = value ?? "Nome não informado.";
}
private string _email;
public string Email
{
get => _email?? "Email não informado.";
private set => _email= value ?? "Email não informado.";
}
private string _denucia;
[Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
public string Denuncia
{
get => _denucia?? "O texto da denuncia não foi encontrado.";
private set => _denucia= value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
}
公共字符串Nome{get=>Nome??“Nome não informado。”
每次你要Nome
时,它都会不断地要求Nome
,不断地要求Nome
,而且……这是因为你的Nome、Email、Denucia的设置者在一个无限的过程中调用自己。顺便说一句,这是一个非常糟糕的主意。如果你写x.Name=null;
,调用时应该是null
使用本地化字符串更糟糕。@Groo不知道为什么?巴西本身就是一个大国,并不是所有软件都需要本地化