当每个对象都传递到 Action<T> 时,如何访问和使用我的 List<dynamic> 中的对象?

How can I access an use the objects in my List<dynamic> when they are each passed into an Action<T>?

抱歉,如果这是重复的,但我找不到它。

这是我的问题的实质。假设我有一个 List<T> 包含动态对象:

var collection = new List<dynamic> 
{
    new { Foo = 1, Bar = "Test Text..."},
    new { Foo = 2, Bar = "Test Text #2..."},
    new { Foo = 3, Bar = "Test Text #3..."},
};

现在,假设我们要迭代这个集合,并将每个 dynamic 传递给 Action<T>。特别是在我的例子中,我需要将我的对象传递给 Parallel.Foreach 调用。我已经能够构建以下代码并且可以正确构建应用程序。

Parallel.ForEach(collection, (s) => 
    { 
        Console.WriteLine("Success!");
    });

当然,在我们对参数进行操作之前,这基本上是没有用的。我需要做的是访问 lambda 中的 s 参数。

像这样:

Parallel.ForEach(collection, (s) => 
    { 
        Console.WriteLine("Foo = {0}, Bar = {1}", s.Foo, s.Bar);
    });

但是,当我尝试编写这样的代码时,出现 VS 错误,指出:

One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?

如何从我的 lambda 表达式中访问我的动态对象?

这是正确的语法。该项目缺少对 Microsoft.CSharp .Net 程序集的引用。 (呸!)