简体   繁体   English

C#的LINQ和.NET Framework,哪一个依赖于另一个?

[英]C#'s LINQ and .NET Framework, which one depends on the other?

I think of C# language compiler as a self contained black box capable of understanding text of a certain syntax and producing compiled code. 我认为C#语言编译器是一个独立的黑盒子,能够理解某种语法的文本并生成编译代码。 On the other hand .NET framework is a massive library that contains functionality written partly by C# and partly by C++. 另一方面,.NET框架是一个庞大的库,包含部分由C#编写的功能,部分由C ++编写。 So .NET framework depends on C# language, not the other way around. 所以.NET框架依赖于C#语言,而不是相反。

But I cannot fit this into how LINQ works. 但我无法理解LINQ的工作原理。 LINQ queries are text of a particular syntax that C# compiler can understand. LINQ查询是C#编译器可以理解的特定语法的文本。 But to build by own LINQ provider I need to work with interfaces like IQueryable and IQueryProvider both of which are defined in System.Linq namespace of the framework. 但是要通过自己的LINQ提供程序构建,我需要使用IQueryable和IQueryProvider等接口,这两个接口都在框架的System.Linq命名空间中定义。

Does that mean a functionality C# language offers is dependent on a part of .NET framework? 这是否意味着C#语言提供的功能依赖于.NET框架的一部分? Does C# language know about .NET framework? C#语言是否了解.NET框架?

.NET Framework contains of many pieces. .NET Framework包含许多部分。 One of the most important is CLR — Common Language Runtime. 其中最重要的是CLR - Common Language Runtime。 All .NET languages depend on it, C# included, because they produce IL-code which cannot be executed by machine processor. 所有.NET语言都依赖于它,包括C#,因为它们产生的IL代码不能由机器处理器执行。 Instead, CLR executes it. 相反,CLR执行它。

And there is also Base Class Library, BCL, which is available to use for every .NET language: C#, VB.NET, Managed C++, F#, IronRuby, you name it. 还有基类库,BCL,可用于每种.NET语言:C#,VB.NET,托管C ++,F#,IronRuby,您可以命名。 I doubt it was written in C#. 我怀疑它是用C#编写的。 It doesn't depend on any features of those languages, because classes and OOP are built in CLR. 它不依赖于这些语言的任何功能,因为类和OOP是在CLR中构建的。

So, yes, C# language knows about .NET framework, it absolutely must know about it. 所以,是的,C#语言了解.NET框架,它绝对必须知道它。 Think about IEnumerable : to compile foreach into GetEnumerator() , and MoveNext() calls, C# compiler has to know that, well, IEnumerable exists. 想想IEnumerable :将foreach编译成GetEnumerator()MoveNext()调用,C#编译器必须知道, IEnumerable存在。 And is somewhat special. 并且有点特别。

Or think about attributes! 或者考虑属性! C# compiler has the intrinsic knowledge about what methods Attribute interface provides. C#编译器具有关于Attribute接口提供的方法的内在知识。

But CLR itself doesn't know anything about C#. 但CLR本身对C#一无所知。 At all. 完全没有。

LINQ queries are text of a particular syntax that C# compiler can understand. LINQ查询是C#编译器可以理解的特定语法的文本。

Well, query expressions are - but the compiler doesn't really "understand" them. 好吧, 查询表达式是 - 但编译器并没有真正“理解”它们。 It just translates them in a pretty mechanical manner. 它只是以一种非常机械的方式翻译它们。 For example, take this query: 例如,请执行以下查询:

var query = from foo in bar
            where foo.X > 10
            select foo.Y;

That is translated into: 这被翻译成:

var query = bar.Where(foo => foo.X > 10)
               .Select(foo => foo.Y);

The compiler doesn't know anything about what Where and Select mean here. 编译器不知道什么东西 Where ,并Select这里的意思。 They don't even have to be methods - if you had appropriate fields or properties of delegate types, the compiler would be fine with it. 它们甚至不必是方法 - 如果你有适当的字段或委托类型的属性,编译器就可以了。 Basically, if the second form will compile, so will the query expression. 基本上,如果第二个表单将编译,查询表达式也将编译。

Most LINQ providers use extension methods to provide these methods ( Where , Select , SelectMany etc). 大多数LINQ提供程序使用扩展方法来提供这些方法( WhereSelectSelectMany等)。 Again, they're just part of the C# language - the compiler doesn't know or care what the extension methods do . 同样,它们只是C#语言的一部分 - 编译器不知道或不关心扩展方法的作用

For more details about how query expressions are translated, see part 41 of my Edulinq blog series . 有关如何翻译查询表达式的更多详细信息,请参阅我的Edulinq博客系列的第41部分 You may find the rest of my Edulinq series informative, too - it's basically a series of blog posts in which I reimplement LINQ to Objects, one method at a time. 您可能会发现我的Edulinq系列的其他部分也非常有用 - 它基本上是一系列博客文章,其中我重新实现LINQ to Objects,一次一种方法。 Again, this demonstrates that the C# compiler doesn't rely on the LINQ implementation being in the System.Linq namespace, or anything like that. 同样,这表明C#编译器不依赖于System.Linq命名空间中的LINQ实现,或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM