【问题标题】:Unable to load class fields into child classes无法将类字段加载到子类中
【发布时间】:2019-08-05 02:40:04
【问题描述】:

第一步

public class Catalog
{
    public int CatalogId { get; set; }
    public string CatalogName { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public bool IsDeleted { get; set; }
}

模型的第二步

public class Catalogs : Catalog { }

public class GetCatalogResponse
{
    public Catalogs Catalogs { get; set; }
}

第三步

var resp = new Models.GetCatalogResponse();

在这种情况下,我无法将目录字段加载到 var resp

【问题讨论】:

  • 您需要初始化并为这些参数分配一些值才能访问。我真的没有得到,你想用它做什么。你可以看看dotnetfiddle.net/UJRnLP

标签: c# asp.net-web-api asp.net-core asp.net-web-api2


【解决方案1】:

如果您想要扩展 Catalog 类以包含自定义字段,请尝试以下操作:

public class Catalog
{
    public int CatalogId { get; set; }
    public string CatalogName { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public bool IsDeleted { get; set; }
}
public class CatalogExtension : Catalog
{
    public string YourCustomValue { get; set; }

    public CatalogExtension(Catalog catalog)
    {
        CatalogId = catalog.CatalogId;
        CatalogName = catalog.CatalogName;
        StartDate = catalog.StartDate;
        EndDate = catalog.EndDate;
        IsDeleted = catalog.IsDeleted;
        YourCustomValue = "test"
    }

    public CatalogExtension()
    {
        // Assign values however you want
    }
}

然后,您可以通过以下方式获取一个新的 CatalogExtension 对象:

var resp = new CatalogExtension(catalog);

var resp = new CatalogExtension();

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2018-07-25
    • 2016-02-26
    相关资源
    最近更新 更多