首页 > 解决方案 > 如何从另一个属性中获取属性值?

问题描述

我有以下界面:

public interface IReport
{
    int ReportId {get; set;}
}

我有一个具有标识列属性的实体:

public int PaymentReportId {get; set;}

我需要PaymentReport实施IReport

在我的PaymentReport.cs 中,我做了:

public int PaymentReportId {get; set;}

public int ReportId {
    get => PaymentReportId; 
    set {} //no need to ever set this;
}

否则编译器会抱怨没有实现 setter。

有没有更清洁的方法来做到这一点?

标签: c#

解决方案


我从 IReport 的 ReportId 中删除了该集合,然后在类中实现。

public interface IReport
{
    int  ReportId { get;  }
}

public class PaymentReport : IReport
{
    public int PaymentReportId { get; set; }
    public int ReportId
    {
        get => PaymentReportId;
    }
}

推荐阅读