简体   繁体   English

如何制作一个遍历列表并以要访问的数据成员作为输入参数的函数

[英]How can I make a function that loops over a list and takes which data member to access as input parameter

I have a datatype PlayerStats which contains a lot of different data members. 我有一个包含很多不同数据成员的数据类型PlayerStats。 I want to calculate a score which is different for each data member (the case below looks at statistics.nrOfGoals). 我想计算一个分数,该分数对于每个数据成员都是不同的(下面的情况是statistics.nrOfGoals)。

private double getScore()
{
    double strength = 0;
    foreach (PlayerStats statistics in this.statistics)
    {
        double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
        dateDiff = Math.Pow(dateDiff, Form1.historyFactor);

        strength += (statistics.nrOfGoals * ValueTable.PointsPerGoals   ) / dateDiff;
    }

    return strength;
}

How can I make this function general and accept which datamember to look at instead of creating a lot of similar looking functions? 我如何才能使此函数具有通用性并接受要查看的数据成员,而不是创建许多相似的函数?

Something like 就像是

private double getScore(Type type, Type type2)
{
    double strength = 0;
    foreach (PlayerStats statistics in this.statistics)
    {
        double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
        dateDiff = Math.Pow(dateDiff, Form1.historyFactor);

        strength += (statistics.type * ValueTable.type2) / dateDiff;
    }

    return strength;
}

You can give a function as a parameter with signature PlayerStats -> Double : 您可以使用签名PlayerStats -> Double来给函数提供参数:

private double getScore(Func<PlayerStats,double> type, double type2)
{
    double strength = 0;
    foreach (PlayerStats statistics in this.statistics)
    {
        double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
        dateDiff = Math.Pow(dateDiff, Form1.historyFactor);

        strength += (type(statistics) * type2) / dateDiff;
    }

    return strength;
}

And then call it with: 然后调用:

getScore(x => x.nrOfGoals,ValueTable.PointsPerGoals);

x => x.nrOfGoals is a lambda-expression that defines some kind of function that (in this case) takes as input a PlayerStats instance and returns a double . x => x.nrOfGoals是一个lambda表达式 ,它定义某种函数(在这种情况下)将PlayerStats实例作为输入并返回double

In the code, you can then see type as a "function"/"method" and call it with type(y) (with y a PlayerStats instance). 然后,在代码中,您可以将type视为“函数” /“方法”,并使用type(y) (带有yPlayerStats实例)对其进行PlayerStats

您可以将属性名称作为字符串参数,并使用反射按名称查找属性。

You can pass Func<PlayerStats, double> to your function, like: 您可以将Func<PlayerStats, double>传递给函数,例如:

private double getScore(Func<PlayerStats, double> evaluator)
{
    double strength = 0;
    foreach (PlayerStats statistics in this.statistics)
    {
        double dateDiff = Math.Abs(nowDate.Subtract(statistics.date).Days / (365.25 / 12));
        dateDiff = Math.Pow(dateDiff, Form1.historyFactor);

        strength += evaluator(statistics) / dateDiff;
    }

    return strength;
}

And then call it like (in your shown case) 然后称呼它(在您显示的情况下)

getScore(x => x.nrOfGoals * ValueTable.PointsPerGoals);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以创建一个实用的方法,它将任意表达式作为参数并调用它吗? - Can I make a utility method which takes an arbitrary expression as a parameter and calls it? 如何访问C#函数,它将指针作为C#的输入参数 - How to access a C++ function which takes pointers as input argument from a C# 如何创建一个采用映射的通用列表和字符串参数的方法? - How to create a method which takes generic list and string parameter with mapping? 绑定到不带参数的成员函数? - Binding to a member function which takes no parameters? 如何从 C# 线程到 object 哪个成员 function 是该线程的参数? - How do I get from C# thread to the object which member function was the parameter to that thread? 我如何在一个 function 中使用该列表,该列表在另一个 function 中添加了一些数据 - How can i use the list in one function which has some data added in another function 当我只能访问列表的对象并且不知道type参数时,如何遍历列表的项目? - How to iterate over items of a list when I only have access to the object of that list and dont know the type parameter? 如何将多个相同的功能类(控制器)组合到一个以数据对象作为参数的类中? - How can I combine multiple same functionality classes (controllers) into one that takes a data object as a parameter? 如何使用本地列表作为函数的输入参数 - How to use local list as an input parameter for function 如何访问主表单的成员 - How can I access member of main form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM