简体   繁体   English

.NET如何解析类型?

[英]How does .NET resolve types?

I am curious how .net resolves types. 我很好奇.net如何解析类型。 I know there is a compile-time component because you can't compile a program that has using statements or fully qualified types which aren't in the current assembly or a referenced assembly. 我知道有一个编译时组件,因为您不能编译具有使用当前程序集或引用程序集中所没有的语句或完全限定类型的程序。 I wonder if it's the compiler that throughs an error or if it's visual studio that throws a build error if it can't find a reference in the proj file? 我想知道是编译器遇到了错误,还是Visual Studio在proj文件中找不到引用时引发了构建错误?

I am also wondering how .net finds the assembly at runtime. 我也想知道.net如何在运行时找到程序集。 I know .net uses a JIT compiler that loads assemblies the first time it runs into a type from that assembly. 我知道.net使用JIT编译器在它第一次从该程序集运行为类型时加载程序集。 I am curious about how it goes about looking for the type and the search order. 我很好奇如何寻找类型和搜索顺序。 Does the assembly manifest tell the runtime exactly where to look for referenced assemblies (like the ASP.NET web.config file)? 程序集清单是否告诉运行时准确在哪里查找引用的程序集(如ASP.NET web.config文件)? Any links to blog posts, books, specs, etc would be much appreciated. 任何对博客文章,书籍,规格等的链接将不胜感激。

The ultimate source of error messages you get when you use an identifier that isn't recognized is the compiler. 当您使用无法识别的标识符时,错误消息的最终来源是编译器。 You'll get an early warning from IntelliSense, it puts the red squiggles under the identifier name. 您将收到IntelliSense的预警,它将红色的花键放在标识符名称下。

The compiler records the assembly, namespace and type name in the metadata of the assembly it generates from your source code. 编译器在从源代码生成的程序集的元数据中记录程序集,名称空间和类型名称。 For the assembly, it records the display name (like System), the [AssemblyVersion] it found in the reference assembly and the PublicKeyToken, a value that's relevant for strong-named assemblies. 对于程序集,它记录显示名称(如System),它在参考程序集中找到的[AssemblyVersion]和PublicKeyToken(与强命名程序集相关的值)。

At runtime, it is the job of the CLR to find the assembly back with just those three assembly properties. 在运行时,CLR的任务是仅使用这三个装配属性来找到装配。 The jitter will be the one that asks the CLR to find it when it needs to generate code. 抖动将是要求CLR在需要生成代码时找到它的抖动。 There are several obscure details about it, associated with overriding the normal lookup rules, you can find them documented in any good book about .NET or MSDN. 关于它,有一些晦涩的细节,它们与覆盖正常的查找规则有关,您可以在任何有关.NET或MSDN的好书中找到它们的文档。 This MSDN article goes into the details. MSDN文章将详细介绍。

The normal way is that it first looks in the GAC, a depository for assemblies and the place where all .NET framework assemblies are stored. 通常的方法是,它首先在GAC中查找,它是程序集的存储库以及所有.NET Framework程序集的存储位置。 The GAC is special since it can store multiple assemblies with the same name but different versions, a strong DLL Hell counter-measure. GAC是特殊的,因为它可以存储具有相同名称但不同版本的多个程序集,这是强大的DLL Hell对策。 If it doesn't find it there, using an exact match with those three properties plus the architecture, it then looks in the directory where the EXE is stored, accepting a match if the display name matches. 如果在该处找不到它,则使用与这三个属性以及体系结构的完全匹配,然后在存储EXE的目录中查找,如果显示名称匹配,则接受匹配。 All is well if all assembly properties match and the type can be found back with the same namespace and type name. 如果所有程序集属性都匹配,并且可以使用相同的名称空间和类型名称找到类型,那么一切都很好。

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

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