如何使用LINQ在C ++ / CLI - 在VS 2010 / .NET 4.0 [英] How to use LINQ in C++/CLI - in VS 2010/.Net 4.0
问题描述
只是不知道是否有一种方法可以使用LINQ在C ++ / CLI。我发现的重点是VS 2008和要求一堆的解决方法系统:: String类的一个职位。我看到的$ C $的CProject一些框架的替代品,但我不知道是否有一种方法可以在C ++ / CLI直接使用它。如果可以的话,任何人有一个很好的例子?
Just wondering if there is a way to use LINQ in C++/CLI. I found one post that was focused on VS 2008 and required a bunch of workarounds for the System::String class. I have seen some framework replacements on CodeProject, but I was wondering if there is a way to use it directly in C++/CLI. If you can, anyone have a good example?
推荐答案
您可以使用在系统:LINQ的
命名空间中定义的LINQ的方法,但你'马上要跳,通过一对额外的篮球。
You can use the Linq methods that are defined in the System::Linq
namespace, but you'll have to jump through a couple extra hoops.
首先,C ++ / CLI不支持扩展方法。然而,扩展方法是对不同类定义的常规方法系统:LINQ的
,这样你就可以直接调用它们。
First, C++/CLI doesn't support extension methods. However, the extension methods are regular methods defined on various classes in System::Linq
, so you can call them directly.
List<int>^ list = gcnew List<int>();
int i = Enumerable::FirstOrDefault(list);
二,C ++ / CLI不支持拉姆达EX pressions。唯一的解决方法是宣布一个实际的方法,并传递作为代表。
Second, C++/CLI doesn't support lambda expressions. The only workaround is to declare an actual method, and pass that as a delegate.
ref class Foo
{
public:
bool GreaterThanZero(int i) { return i > 0; }
void Bar()
{
List<int>^ list = gcnew List<int>();
int i = Enumerable::FirstOrDefault(list, gcnew Func<int, bool>(&Foo::GreaterThanZero));
}
}
这篇关于如何使用LINQ在C ++ / CLI - 在VS 2010 / .NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!