why the property in the derived class is not accessible here?

Jesse Li 0 Reputation points
2024-10-27T16:03:57.3133333+00:00

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 ?)

            }

        });

    }

}
C#
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
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,736 Reputation points
    2024-10-27T19:30:59.4+00:00

    Two things:

    • Your 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.
    • The 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;
    
    				}
    
    			});
    
    	}
    
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.