gpt4 book ai didi

c# - 没有给出与 'id' 所需的形参 'Human.Human(int, string, string, string)' 相对应的参数

转载 作者:行者123 更新时间:2023-12-03 04:35:02 27 4
gpt4 key购买 nike

我无法从 Homecontrollers 操作方法调用新的 Human 对象。

        var employee = new Human { id = 1, name = "home index" , isAuth = isAuth,token="null"};

这是我的模型

namespace WebApplication3.Models
{
public class Human
{
public Human(int id, string name, string isAuth, string token)
{
this.id = id;
this.name = name;
this.isAuth = isAuth;
this.token = token;
}

最佳答案

您正在使用 Object Initializer 的语法但这种语法至少需要使用一个空的构造函数,因为大括号内的代码是在构造对象之后执行的。

如果您想使用该语法,您需要将一个空构造函数添加到您的模型中

public class Human
{
public Human() {}

public Human(int id, string name, string isAuth, string token)
{
this.id = id;
this.name = name;
this.isAuth = isAuth;
this.token = token;
}

现在你有两个选择。使用当前语法,以便编译器调用空构造函数,然后使用大括号之间的值初始化属性,或者使用正常语法直接调用具有四个参数的构造函数。

var employee = new Human (1,"home index",isAuth,"null");

请注意,对于第一种方法,您需要将这些属性公开

public class Human
{
public int id {get;set;}
.....

关于c# - 没有给出与 'id' 所需的形参 'Human.Human(int, string, string, string)' 相对应的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54195703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com