簡體   English   中英

C#:如何將類屬性公開給一個類,而不公開給另一個類?

[英]C#: How can I expose a class property to one class but not to another?

我的類庫中有一個數據協定“ StudentInformation”,如下所示:

public class StudentInformation
{
    public int StudentId { get; set; }
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

Request模型發送StudentId,類庫處理信息並填充屬性“ CanChangeBus”和“ AvailableBuses”,然后在響應模型中將其返回。

我想從請求模型中隱藏屬性“ CanChangeBus”和“ AvailableBuses”。 如果我將這些屬性的設置器更改為“內部”,則無法通過調用應用程序來設置屬性,但它們仍然可見。 如何隱藏它們,使其不調用應用程序的請求模型實例?

public class BasicStudentInformation
{
    public int StudentId { get; set; }
}

public class StudentInformation : BasicStudentinformation
{
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public BasicStudentInformation StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

使用繼承。 就像是:

public class StudentBase
{
    public int StudentId { get; set; }
}

public class StudentInformation : StudentBase
{
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public StudentBase StudentInfo { get; set; }
    ...
    ...
}

public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
    ...
}

因此,BusChangeRequest僅需要查看ID,然后僅應具有ID。

public class StudentInformation
{
    public int StudentId { get; set; }
    public bool CanChangeBus { get; set; }
    public List<int> AvailableBuses { get; set; }
}

public class BusChangeRequestModel
{
    public int StudentId { get; set; }
}

//I don't know what is expected as a repsonse?  Is StudentInfromation really the correct response
//or is something like "Accepted", "rejected", Fail...???
public class BusChangeResponseModel
{
    public StudentInformation StudentInfo { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM