C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
public class LoanPerformance
{
public decimal Principal { get; set; }
}
public class AggregatedPerformance
{
public int LoanId { get; set; }
public decimal Principal { get; set; }
public virtual void Aggregate<TLoanPerformance, TAggregated>(IEnumerable<TLoanPerformance> loanPerformances, Action<TLoanPerformance> accumulator)
where TLoanPerformance : LoanPerformance
where TAggregated : AggregatedPerformance, new()
{
foreach (TLoanPerformance loanPerformance in loanPerformances)
{
this.Principal += loanPerformance.Principal;
accumulator(loanPerformance);
}
}
public void Aggregate<TLoanPerformance, TAggregated>(IEnumerable<TLoanPerformance> loanPerformances)
where TLoanPerformance : LoanPerformance
where TAggregated : AggregatedPerformance, new()
{
Aggregate<TLoanPerformance, TAggregated>(loanPerformances, _ => { });
}
}
public class CreditCardPerformance : LoanPerformance
{
public decimal CreditLimit { get; set; }
}
public class CCAggregatedPerformance : AggregatedPerformance
{
public decimal CreditLimit { get; set; }
public override void Aggregate<CreditCardPerformance, CCAggregatedPerformance>(IEnumerable<CreditCardPerformance> loanPerformances, Action<CreditCardPerformance> accumulator)
{
base.Aggregate<CreditCardPerformance, CCAggregatedPerformance>(loanPerformances,
(_perf) =>
{
var creditCardPerf = _perf as CreditCardPerformance;
if (creditCardPerf != null)
{
this.CreditLimit += creditCardPerf.CreditLimit; // Error CS1061 'AggregatedPerformance' does not contain a definition for 'CreditLimit' and no accessible extension method 'CreditLimit' accepting a first argument of type 'AggregatedPerformance' could be found(are you missing a using directive or an assembly reference ?)
}
});
}
}
Two things:
override
of Aggregate
in CCAggregatedPerformance
has differently named type arguments than those defined in the base class AggregatedPerformance
. Not a technical issue, but denotes an assumption that the type passed can't be another derived type of LoanPerformance
when it is possible with the constraints applied in the base class.override
of Aggregate
also had a name collision between the first type argument (CreditCardPerformance
) and the class CreditCardPerformance
. Renaming this type argument resolves the issue.Updated:
public class LoanPerformance {
public decimal Principal { get; set; }
}
public class AggregatedPerformance {
public int LoanId { get; set; }
public decimal Principal { get; set; }
public virtual void Aggregate<TLoanPerformance, TAggregated>(IEnumerable<TLoanPerformance> loanPerformances, Action<TLoanPerformance> accumulator)
where TLoanPerformance : LoanPerformance
where TAggregated : AggregatedPerformance, new() {
foreach (TLoanPerformance loanPerformance in loanPerformances) {
this.Principal += loanPerformance.Principal;
accumulator(loanPerformance);
}
}
public void Aggregate<TLoanPerformance, TAggregated>(IEnumerable<TLoanPerformance> loanPerformances)
where TLoanPerformance : LoanPerformance
where TAggregated : AggregatedPerformance, new() {
Aggregate<TLoanPerformance, TAggregated>(loanPerformances, _ => { });
}
}
public class CreditCardPerformance : LoanPerformance {
public decimal CreditLimit { get; set; }
}
public class CCAggregatedPerformance : AggregatedPerformance {
public decimal CreditLimit { get; set; }
public override void Aggregate<TLoanPerformance, TAggregated>(IEnumerable<TLoanPerformance> loanPerformances, Action<TLoanPerformance> accumulator) {
base.Aggregate<TLoanPerformance, CCAggregatedPerformance>(loanPerformances,
(_perf) => {
var creditCardPerf = _perf as CreditCardPerformance;
if (creditCardPerf != null) {
this.CreditLimit += creditCardPerf.CreditLimit;
}
});
}
}