如何在 C++/CLI 中使用 LINQ - 在 VS 2010/.Net 4.0 中
只是想知道是否有办法在 C++/CLI 中使用 LINQ。我发现一篇文章专注于 VS 2008,并且需要针对 System::String 类的一系列解决方法。我在 CodeProject 上看到了一些框架替换,但我想知道是否有办法直接在 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 System::Linq 命名空间中定义的 Linq 方法,但您必须跳过一些额外的步骤。
首先,C++/CLI 不支持扩展方法。但是,扩展方法是在 System::Linq 中的各个类上定义的常规方法,因此您可以直接调用它们。
其次,C++/CLI 不支持 lambda 表达式。唯一的解决方法是声明一个实际的方法,并将其作为委托传递。
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.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.Second, C++/CLI doesn't support lambda expressions. The only workaround is to declare an actual method, and pass that as a delegate.
您是在谈论“语言集成查询”还是
System::Linq
命名空间?我认识的每个程序员都更喜欢函数调用语法而不是 LINQ 语法。C++/CLI 不支持 LINQ 语法。数据库过去支持一种称为嵌入式 SQL 的语言集成查询形式,但现在它几乎已经消失了。嵌入式 SQL(以及后来的 LINQ-to-SQL)一开始就是一个愚蠢的想法,后来人们发现数据库查询逻辑应该在数据库中,而不是混合到业务逻辑中。
LINQ 到对象是一个更有用的想法,但 SQL 语法感觉不太合适。所以C#程序员倾向于直接调用LINQ库函数。
C++ 并不真正需要 LINQ,因为我们有模板。由模板实现的标准库算法是 LINQ 优点的超集:它们可以专门用于特定容器,但您无需容器类的任何帮助即可获得良好的默认实现。而且它们编译成更高效的代码,因为重载解析发生在专门化之后(与泛型不同)。好吧,模板在运行时反射方面不如泛型那么好,但 C# 扩展方法在运行时反射方面也不能很好地发挥作用。 C++ 标准算法的最大缺点是编写谓词函子的冗长,但 C++0x 引入了 lambda 来解决这个问题。
C++/CLI 真正需要的是适用于 .NET 容器的标准算法版本。 就在这里。例如,LINQ 的
Where
方法与find_if
非常接近。现在我们只需要微软快点实现最终的C++0x规范。Are you talking about "Language Integrated Query" or the
System::Linq
namespace? Every programmer I know prefers the function call syntax instead of LINQ syntax.C++/CLI does not support LINQ syntax. Databases have supported a form of language integrated query in the past, called Embedded SQL, which is pretty much dead these days. Embedded SQL (and later LINQ-to-SQL) was a dumb idea to begin with, people have since figured out that database query logic should be in the database and not mixed into the business logic.
LINQ-to-objects is a more useful idea, but SQL syntax just feels out of place. So C# programmers tend to call the LINQ library functions directly.
C++ doesn't really need LINQ, because we have templates. The standard library algorithms made possible by templates are a superset of the advantages of LINQ: They can be specialized for particular containers, but you get a good default implementation without any help from the container class. And they compile to much more efficient code, because overload resolution happens after specialization (unlike generics). Ok, templates aren't as good for runtime reflection as generics, but C# extension methods don't play well with runtime reflection either. The biggest drawback of the C++ standard algorithms has been the verbosity of writing predicate functors, but C++0x introduces lambdas which take care of that.
Really what C++/CLI needs is a version of the standard algorithms that works on .NET containers. And here it is. For example, LINQ's
Where
method corresponds pretty closely tofind_if
. Now we just need Microsoft to hurry up and implement the final C++0x spec.