列表<t>的变​​量更改

时间:2016-08-18 00:59:48

标签: c# list

所以我是c#的新手,因此之前从未真正使用过list。到目前为止,我已经从中获得了许多好东西,但我遇到了一个障碍。

public static List<player> data(string ply, int val)
    {
        string rtn = "-";
        List<player> playerdata = new List<player>()
        {
            new player() { plr = "player1", name = "pl A", one = 0, two = 0, twopair = 0 },
            new player() { plr = "player2", name = "pl B", one = 0, two = 0, twopair = 1 }
        };

        var game = playerdata.Where(p => p.plr == ply);

        foreach (var player in game)
        {
            player.one = val; //This is where my problem is
            rtn = player.name;
        }
        return playerdata;
    }

所以在foreach下,使用player.et = val; ..工作正常。但我真的希望能够拥有&#34; player.et&#34;作为动态输入。所以我可以发送

data(string ply, int val, SOMETHING HERE)

链接到

foreach (var player in game)
    {
        SOMETHING HERE = val;
        rtn = player.name;
    }

我该怎么做?

先谢谢:D

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

public static List<player> data(string ply, string property, int val)
{
    string rtn = "-";

    List<player> playerdata = new List<player>()
    {
        new player() { plr = "player1", name = "pl A", one = 0, two = 0, twopair = 0 },
        new player() { plr = "player2", name = "pl B", one = 0, two = 0, twopair = 1 }
    };

    playerdata.Where(p => p.plr == ply).ToList().ForEach(p => {
        var propInfo = p.GetType().GetProperty(property);
        if (propInfo != null)
            propInfo.SetValue(p, val, null);
        rtn = p.name;
    });

    return playerdata;
}
相关问题