我如何传递一个类的属性作为一种方法的参数? [英] How can I pass a property of a class as a parameter of a method?

查看:158
本文介绍了我如何传递一个类的属性作为一种方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了十几性质,代表各种金融领域的一类。我有另一个类,需要在每个这些领域中分别执行一些计算。这些计算方法里面的代码是除了它确实对计算领域相同的。

I have a class that has a dozen or so properties that represent various financial fields. I have another class that needs to perform some calculations on each of those fields separately. The code inside those calculation methods are identical except for the field that it does the calculation on.

有没有办法,我可以通过一个属性名作为参数,只是一种方式有一个方法,做了所有的演艺工作,而不是每个属性的12方法?

Is there a way that I can pass a property name as a parameter and just have one method that does all of the performing work instead of the 12 methods for each property?

另外,我敢肯定,这可以通过反射来实现,但我见过在lambda表达式都在这同一种时尚的使用,想知道如果这是在此可以使用的候选其他代码。

Also, I'm sure this can be accomplished via reflection, but I've seen in other code where lambdas are used in this same kind of fashion and was wondering if this is a candidate where this can be used.

按照要求,这里是一个例如:

As requested, here is an example:

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
}

我的目标是只是让所谓的GetTotalAmountByDate(一种方法),并通过在开始日期和结束日期和属性的名称(或ReleasedFederalAmount ReleasedLocalAmount等。 )需要访问。

My goal is to just make one method called GetTotalAmountByDate() and pass in a begin date, and end date and the name of the property (ReleasedFederalAmount or ReleasedLocalAmount, etc.) it needs to access.

我希望这准确地描述了什么,我试图完成。

I hope this depicts accurately what I'm trying to accomplish.

谢谢!

推荐答案

您不需要思考,如果你的属性是所有的数字,可以作为单一类型的均匀处理 - 让我们说。一个小数

You don't need reflection if your properties are all numeric and can be homogeneously treated as a single type - let's say a decimal.

这样的事情应该做的伎俩:

Something like this should do the trick:

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;
}

您可以再提供适当命名的版本为您所有的具体情况:

You can then provide appropriately named versions for all of your specific cases:

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

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

// other versions ....

这篇关于我如何传递一个类的属性作为一种方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆