创建继承接口的对象
Create object that inherit the interface
我有两个模型接口ILoanModel
和IAmortizationModel
。
public interface ILoanModel
{
string ID { get; set; }
IEmployeeModel Borrower { get; set; }
// ******* other propeties *********
ICollection<Model.IAmortizationModel> AmortizationTable { get; set; }
}
public interface IAmortizationModel
{
string ID { get; set; }
ILoanModel Loan { get; set; }
// ******* other propeties *********
}
我创建了另一个接口来处理计算和其他功能
public interface IAmortization
{
// Compute and generate amortization table
Model.ILoanModel Generate(Model.ILoanModel loan);
// **** Other function *****
}
目前我有 3 种类型的贷款,它们都有相同的计算方式和摊还方式table。
public abstract class Amortization : Controller.Computation.Interface.IAmortization
{
public virtual Model.ILoanModel Generate(Model.ILoanModel loan)
{
// **** other code *******
//Create the amortization table
decimal last_Amortization_RunningBalance = loan.Amount;
loan.AmortizationTable = new List<Model.IAmortizationModel>();
for (int x = 1; loan.NumberOfPayment >= x; x++)
{
// having problem here because Model.IAmortizationModel is a interface
var newAmortizationRow = new Model.IAmortizationModel
{
ID = Guid.NewGuid().ToString(),
AmortizationAmount = loan.AmortizationPaymentAmount,
AmortizationInterest = loan.AmortizationPaymentAmount* loan.Nominal,
AmortizationPrepayment =
loan.AmortizationPaymentAmount
- (loan.AmortizationPaymentAmount * loan.Nominal),
AmortizationOutstandingBalance =
last_Amortization_RunningBalance
- (loan.AmortizationPaymentAmount
- (loan.AmortizationPaymentAmount * loan.Nominal)),
Sequence = x
};
last_Amortization_RunningBalance -=
newAmortizationRow.AmortizationOutstandingBalance;
loan.AmortizationTable.Add(newAmortizationRow);
}
}
}
我现在的问题是生成将添加到 loan.AmortizationTable
的新 IAmortizationModel
。如何获取 loan.AmortizationTable
的类型并创建?
一个选择是使其成为通用的并添加一个通用约束 new()
:
public virtual Model.ILoanModel Generate<T>(Model.ILoanModel loan)
where T:IAmortizationModel, new()
然后调用new T()
另一种选择是传递生成器函数:
public virtual Model.ILoanModel Generate(Model.ILoanModel loan,
Func<IAmortizationModel> generator)
并调用 generator()
我有两个模型接口ILoanModel
和IAmortizationModel
。
public interface ILoanModel
{
string ID { get; set; }
IEmployeeModel Borrower { get; set; }
// ******* other propeties *********
ICollection<Model.IAmortizationModel> AmortizationTable { get; set; }
}
public interface IAmortizationModel
{
string ID { get; set; }
ILoanModel Loan { get; set; }
// ******* other propeties *********
}
我创建了另一个接口来处理计算和其他功能
public interface IAmortization
{
// Compute and generate amortization table
Model.ILoanModel Generate(Model.ILoanModel loan);
// **** Other function *****
}
目前我有 3 种类型的贷款,它们都有相同的计算方式和摊还方式table。
public abstract class Amortization : Controller.Computation.Interface.IAmortization
{
public virtual Model.ILoanModel Generate(Model.ILoanModel loan)
{
// **** other code *******
//Create the amortization table
decimal last_Amortization_RunningBalance = loan.Amount;
loan.AmortizationTable = new List<Model.IAmortizationModel>();
for (int x = 1; loan.NumberOfPayment >= x; x++)
{
// having problem here because Model.IAmortizationModel is a interface
var newAmortizationRow = new Model.IAmortizationModel
{
ID = Guid.NewGuid().ToString(),
AmortizationAmount = loan.AmortizationPaymentAmount,
AmortizationInterest = loan.AmortizationPaymentAmount* loan.Nominal,
AmortizationPrepayment =
loan.AmortizationPaymentAmount
- (loan.AmortizationPaymentAmount * loan.Nominal),
AmortizationOutstandingBalance =
last_Amortization_RunningBalance
- (loan.AmortizationPaymentAmount
- (loan.AmortizationPaymentAmount * loan.Nominal)),
Sequence = x
};
last_Amortization_RunningBalance -=
newAmortizationRow.AmortizationOutstandingBalance;
loan.AmortizationTable.Add(newAmortizationRow);
}
}
}
我现在的问题是生成将添加到 loan.AmortizationTable
的新 IAmortizationModel
。如何获取 loan.AmortizationTable
的类型并创建?
一个选择是使其成为通用的并添加一个通用约束 new()
:
public virtual Model.ILoanModel Generate<T>(Model.ILoanModel loan)
where T:IAmortizationModel, new()
然后调用new T()
另一种选择是传递生成器函数:
public virtual Model.ILoanModel Generate(Model.ILoanModel loan,
Func<IAmortizationModel> generator)
并调用 generator()