c# - 使用 Lambda 和 Func<> 的动态构造函数

标签 c# linq design-patterns func

我有一个多线程应用程序,它在 BlockingCollection 队列上创建一个字符串列表,我想获取该字符串列表并通过一个或两个步骤将其转换为项目对象的集合

是否可以创建一个 func<> 或 lamda 方法来实现这种类型的结果

 public class item
{
    public string name { get; set; }

    public item(string nam)
    {
        name = nam;
    }


}

IList<string> alist = new string[] {  "bob","mary"};

从哪里获取 Ilist<> 或 IEnumerable<> 类型的字符串并返回 IList

所以对于单项Func<>

Func<string, item> func1 = x => new item(x);

但基本上签名看起来像

Func<IEnumerable<string>,IList<item>> func2 = x=> x.ForEach(i => func1(i));

我是想在方形孔中放一个圆钉还是我的语法/逻辑错了

提前致谢

最佳答案

你只是想“ reshape ” IList<string>作为IList<item>

IList<string> listOfStrings = new string[] {  "bob","mary"};
IList<item> listOfItems = listOfStrings.Select(s => new item(s)).ToList();

关于c# - 使用 Lambda 和 Func<> 的动态构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8450236/

相关文章:

c# - MVVM中事件的处理方式

c# - 带有 RGiesecke.DllExport 的 C# DLL 中没有函数

c# - 在 LAN 系统上生成备份和还原 SQL 数据库

c# - 有没有一种方法可以在 C#/LINQ 中产生、发出或以其他方式实现以前为空的数组?

linq - NHibernate 从 LINQ 结果中删除

c# - 什么时候返回延迟的 IEnumerable<T> 是错误的形式

java - 哪种设计模式以及如何使用 OOP 进行设计这个场景

java - 将对象转换为字节/从字节转换的设计模式

c# - 鼠标双击仅在右键单击时触发

java - 如何有效地使用事件总线?