如何解决用匿名类型C#调用泛型方法
| 假设我想遍历string[][]
,使用匿名类型附加值,然后对结果执行通用的ForEach-Extension方法(我知道,这是个很棒的例子,但是我想您会得到简而言之!)。
这是我的代码:
//attrs = some string[][]
attrs.Select(item => new { name = HttpContext.GetGlobalResourceObject(\"Global\",item[0].Remove(0,7)),value = item[1] })
.ForEach</*????*/>(/*do stuff*/);
我到底要在ForEach的Type-Parameter中输入什么?
这是ForEach的样子:
public static void ForEach<T>(this IEnumerable<T> collection,Action<T> act)
{
IEnumerator<T> enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
act(enumerator.Current);
}
解决方法
您无需显式指定类型,因为可以从提供的参数中推断出它:
attrs.Select(item => new
{
name = HttpContext.GetGlobalResourceObject(\"Global\",item[0].Remove(0,7)),value = item[1]
})
.ForEach(x => x.name = \"something\");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。