gpt4 book ai didi

C# 代码重复 : property List treatment

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:11 25 4
gpt4 key购买 nike

我有一个浏览对象列表(流派、国籍等)的功能。它必须返回特定属性的添加(每个对象不同)。我无法修改这些列出的对象的代码(在 DDL 中)。

这里是我现在必须做的一些例子:

    private String formatListString(List<Genre> list)
{
for (...)
{
str += list[i].Value;
...
}
return str;
}

private String formatListString(List<Nationality> list)
{
for (...)
{
str += list[i].Code;
...
}
return str;
}

如何在一个函数中实现它?也许在函数中添加一个参数来指定要使用的属性?

谢谢。

最佳答案

return String.Join("...", list.Select(o => o.Code));

如果这对你不起作用,你可以使用泛型和 lambda:

private string FormatList<T>(IEnumerable<T> list, Func<T, String> prop)

关于C# 代码重复 : property List treatment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com