如何将类的属性作为方法的参数传递?

Chr*_*way 5 c# lambda

我有一个类,有十几个属性代表各种金融领域.我有另一个类需要分别对每个字段执行一些计算.这些计算方法中的代码是相同的,除了它进行计算的字段.

有没有办法可以将属性名称作为参数传递,只有一个方法可以执行所有执行工作而不是每个属性的12个方法?

此外,我确信这可以通过反射来完成,但我已经在其他代码中看到lambda以同样的方式使用,并且想知道这是否是可以使用它的候选者.

根据要求,这是一个例子:

public class FinancialInfo
{
    public virtual DateTime AuditDate { get; set; }
    public virtual decimal ReleasedFederalAmount { get; set; }
    public virtual decimal ReleasedNonFederalAmount { get; set; }
    public virtual decimal ReleasedStateAmount { get; set; }
    public virtual decimal ReleasedLocalAmount { get; set; }
    public virtual decimal ReleasedPrivateAmount { get; set; }
    // more fields like this
}

public class FinancialLedger()
{
    public virtual DateTime? BeginDate { get; set; }
    public virtual DateTime? EndDate { get; set; }
    public virtual IList<FinancialInfo> Financials { get; set; } //not actual implementation, but you get the idea
    public decimal GetTotalReleasedFederalAmountByDate()
    {
        if (BeginDate == null && EndDate == null)
            return 0;
        decimal total = 0;
        foreach (var fi in Financials)
        {
            if (someCondition)
                if (someSubCondition)
                    total += fi.ReleasedFederalAmount;
            else if (someOtherCondition)
                if (someOtherSubCondition)
                    total += fi.ReleasedFederalAmount;
            else if (anotherCondigion)
                total += fi.ReleasedFederalAmount;
        }
        return total;
    }
    public decimal GetTotalReleasedNonFederalAmountByDate()
    {
        // same logic as above method, 
        // but it accesses fi.ReleasedNonFederalAmount;
    }
    // More methods the same as the previous, just accessing different
    // members of FinancialInfo
}
Run Code Online (Sandbox Code Playgroud)

我的目标是只创建一个名为GetTotalAmountByDate()的方法,并传入开始日期,结束日期以及它需要访问的属性名称(ReleasedFederalAmount或ReleasedLocalAmount等).

我希望这能准确地描绘出我想要实现的目标.

LBu*_*kin 5

如果您的属性都是数字属性并且可以被均匀地视为单一类型,则不需要反射 - 让我们说a decimal.

像这样的东西应该做的伎俩:

protected decimal ComputeFinancialSum( DateTime? beginDate, DateTime? endDate,
                                       Func<FinancialInfo,decimal> propertyToSum )
{
    if (beginDate == null && endDate == null)
        return 0;
    decimal total = 0;
    foreach (var fi in Financials)
    {
        if (someCondition)
            if (someSubCondition)
                total += propertyToSum(fi);
        else if (someOtherCondition)
            if (someOtherSubCondition)
                total += propertyToSum(fi);
        else if (anotherCondigion)
            total += propertyToSum(fi);
    }
    return total;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以为所有特定情况提供适当命名的版本:

public decimal GetTotalReleasedFederalAmountByDate()
{
    return ComputeFinancialSum( BeginDate, EndDate, 
                                (x) => x.ReleasedFederalAmount );
}

public decimal GetTotalReleasedNonFederalAmountByDate()
{
    return ComputeFinancialSum( BeginDate, EndDate, 
                                (x) => x.ReleasedNonFederalAmount );
}

// other versions ....
Run Code Online (Sandbox Code Playgroud)