why the derived properties can't be accessed?

litn2018

New member
Joined
Oct 27, 2024
Messages
1
Programming Experience
10+
this code has cs1061 error, even just use what github copilot suggested code:
C#:
    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  'CreditCardPerformance' does not contain a definition for 'CreditLimit' and no accessible extension method 'CreditLimit' accepting a first argument of type 'CreditCardPerformance' could be found(are you missing a using directive or an assembly reference ?)
                }
            });
        }
    }
 
Last edited by a moderator:
That is because it is hallucinating. It thinks that it declared a CreditLimit property on the CCAggregatePerformance class, but it didn't.

Of course you should recognize this as you should be the programmer who is reviewing the code it generated, and not simply just accepting what it provides as accurate and/or correct.


NVM. I'm the one hallucinating trying to read code on my phone... :)
 
Last edited:
No time for details right now, but short answer is C# generics is not the same as C++ templates.
 
In CCAggregatedPerformance change the generic type parameter names to avoid conflict with the type CreditCardPerformance:
C#:
public override void Aggregate<T1, T2>(IEnumerable<T1> loanPerformances, Action<T1> accumulator)
{           
    base.Aggregate<T1, T2>(loanPerformances, (_perf) =>
    {
        if (_perf is CreditCardPerformance creditCardPerf)
        {
            this.CreditLimit += creditCardPerf.CreditLimit;
        }
    });
}
 
Not related to the problem, but is TAggregated even used?
 

Latest posts

Back
Top Bottom