我有一个LoanWithClient
继承自Loan
的模型:
public class LoanWithClient : Loan
{
public Client Client { get; set; }
}
如何访问整个继承的Loan
对象,而不必显式地编写其属性?
LoanWithClient不包含贷款的定义
return new LoanWithClient
{
**Loan** = loan, //The Loan is erroring: LoanWithClient does not contain a definition for Loan
Client = client
};
分类贷款:
public class Loan
{
public int ID { get; set; }
public string Address { get; set; }
public string City { get; set; }
//etc..
}
答案 0 :(得分:5)
类LoanWithClient
继承自Loan
。这意味着子类具有父类的所有属性。但这并不意味着子类包含父类作为属性。您可以像这样编写类-
public class Loan
{
public int ID { get; set; }
public string Address { get; set; }
public string City { get; set; }
//etc..
}
public class LoanWithClient
{
public Loan Loan { get; set; }
public Client Client { get; set; }
}
return new LoanWithClient
{
Loan = loan,
Client = client
};
如果要保留您的类体系结构,可以按照以下方式返回-
return new LoanWithClient
{
ID = loan.ID,
Address = loan.Address,
City = loan.City,
//etc..
Client = client
};
答案 1 :(得分:4)
您要
访问继承的成员
贷款不是成员,而是父母。 像这样访问贷款的成员:
return new LoanWithClient
{
ID = loan.ID,
Address = loan.Address,
City = loan.City,
//etc...
Client = client
};