简体   繁体   English

.Net中的执行上下文和仅反射上下文是什么?

[英]What are execution context and reflection-only context in .Net?

I am having a hard time understanding the concepts execution context and reflection-only context in .Net. 我很难理解.Net中的概念执行上下文仅反射上下文 From what I understand these are two different ways to load assemblies, but I can not define them clearly. 据我了解,这是加载程序集的两种不同方式,但是我无法明确定义它们。

About execution context I was able to find the documentation of the ExecutionContext class on msdn and about the reflection-only context, only how to load assemblies into it. 关于执行上下文,我能够找到msdn上ExecutionContext的文档以及有关仅反射上下文的信息,只是如何将程序集加载到其中。

Can somebody provide a description of these two concepts? 有人可以提供这两个概念的描述吗?

A .NET assembly contains many things. .NET程序集包含许多内容。 Generally, they contain types (classes et al) and metadata (information about what those types look like, and much more). 通常,它们包含类型(类等)和元数据(有关这些类型的信息,等等)。

You access metadata using reflection. 您可以使用反射访问元数据。

If you're only interested in the metadata, and not in actually using the types from an assembly, you can load said assembly in a reflection-only context. 如果您对元数据感兴趣,而不对实际使用程序集中的类型感兴趣,则可以在仅反射的上下文中加载该程序集。 This enables you to load and inspect assembly metadata, but not actually instantiate types from that assembly. 这使您可以加载和检查程序集元数据,但实际上不能实例化该程序集的类型。

A good resource about all this is CLR via C# by Jeffrey Richter . 关于这一切的一个很好的资源是Jeffrey Richter通过C#编写的CLR For some short info, see MSDN Blogs: Reflection Only Assembly Loading . 有关一些简短信息,请参见《 MSDN博客:仅反射程序集加载》

Also a good read is this framework documentation: Assemblies in the Common Language Runtime -> Assembly Contents : 该框架文档也是一个不错的阅读:公共语言运行时中的汇编->汇编内容

In general, a static assembly can consist of four elements: 通常,静态程序集可以包含四个元素:

  • The assembly manifest, which contains assembly metadata. 程序集清单,其中包含程序集元数据。

  • Type metadata. 键入元数据。

  • Microsoft intermediate language (MSIL) code that implements the types. 实现类型的Microsoft中间语言(MSIL)代码。

  • A set of resources. 一组资源。

And How to: Load Assemblies into the Reflection-Only Context : 以及如何:将程序集加载到仅反射的上下文中

The reflection-only load context allows you to examine assemblies compiled for other platforms or for other versions of the .NET Framework. 仅反射加载上下文使您可以检查为其他平台或其他版本的.NET Framework编译的程序集。 Code loaded into this context can only be examined; 加载到此上下文中的代码只能进行检查; it cannot be executed. 它无法执行。 This means that objects cannot be created, because constructors cannot be executed. 这意味着无法创建对象,因为无法执行构造函数。 Because the code cannot be executed, dependencies are not automatically loaded. 由于无法执行代码,因此不会自动加载依赖项。 If you need to examine them, you must load them yourself. 如果需要检查它们,则必须自己加载它们。

So the reflection-only context is not really a different process, or a different thread, or anything else, but just a way of loading assemblies without loading the code or dependencies therein. 因此,仅反射上下文实际上不是一个不同的进程,一个线程或其他任何东西,而只是一种加载程序集而不在其中加载代码或依赖项的方式。 And the Assembly class instance representing the loaded assembly will be flagged as such using its Assembly.ReflectionOnly property . 代表已加载程序集的Assembly类实例将使用其Assembly.ReflectionOnly属性进行标记。

It just loads the type metadata , so you can inspect what the types in that assembly look like. 它只是加载类型元数据 ,因此您可以检查程序集中的类型。

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

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