dotNet Assembly Basic

dotNet程序集加载机制
本文介绍了dotNet中程序集的特性和加载机制,包括如何使用不同方法显式加载程序集,程序集如何被隐式加载,以及如何利用反射来获取程序集信息。此外还详细解释了全局程序集缓存的概念及其用途。

dotNet Assembly basic (a overall topic Assemblies and the Global Assembly Cache can be found in MSDN)

Assemblies have the following properties:

  1. Assemblies are implemented as .exe or .dll files.

  2. You can share an assembly between applications by placing it in the Global Assembly Cache.

  3. Assemblies must be strong-named before they can be placed in the Global Assembly Cache. For more information, see Strong-Named Assemblies.

  4. Assemblies are only loaded into memory if they are required. When loading a assembly, its dependency assemblies specified by references will be loaded automatically (resource dll loading follows the same rule).

  5. You can programmatically obtain information about an assembly using reflection. For more information, see the topic Reflection.

  6. If you want to load an assembly only to inspect it, use a method such as ReflectionOnlyLoadFrom.

  7. You can use two versions of the same assembly in a single application. For more information, see extern alias.

How to: Load Assemblies into an Application Domain 

Loading when required means implicit assembly loading. If you want to explicitly load a dotNet assembly, there are many choices to do. The most suggested one is to use System.Reflection.Assembly.Load. 

There are several ways to load an assembly into an application domain. The recommended way is to use the static (Shared in Visual Basic) Load method of the System.Reflection.Assembly class. Other ways assemblies can be loaded include:

  1. The LoadFrom method of the Assembly class loads an assembly given its file location. Loading assemblies with this method uses a different load context.

  2. The ReflectionOnlyLoad and ReflectionOnlyLoadFrom methods load an assembly into the reflection-only context. Assemblies loaded into this context can be examined but not executed, allowing the examination of assemblies that target other platforms. See How to: Load Assemblies into the Reflection-Only Context.

NoteNote

The reflection-only context is new in the .NET Framework version 2.0.

  1. Methods such as CreateInstance and CreateInstanceAndUnwrap of the AppDomain class can load assemblies into an application domain.

  2. The GetType method of the Type class can load assemblies.

  3. The Load method of the System.AppDomain class can load assemblies, but is primarily used for COM interoperability. It should not be used to load assemblies into an application domain other than the application domain from which it is called.

NoteNote

Starting with the .NET Framework version 2.0, the runtime will not load an assembly that was compiled with a version of the .NET Framework that has a higher version number than the currently loaded runtime. This applies to the combination of the major and minor components of the version number.

You can specify the way the just-in-time (JIT) compiled code from loaded assemblies is shared between application domains. For more information, see Application Domains and Assemblies.

C++ VS dotNet

Both support implicit / explicit assembly loading:

  1. Implicit assembly loading. C++ doesn't support loading resource dll in that way, while C# supports.
  2. Explicit assembly loading. C++ uses LoadLibrary while C# mainly uses Assembly.Load.

Global Assembly Cache(MSDN link)

It's a machine-wide code cache where to store assemblies that intend to be shared between applications. dotNet assemblies should always be deployed there; and you can install your assemblies into GAC by gacutil.exe mainly. You can easily find steps to action on it in detail by google.

 

Each computer where the common language runtime is installed has a machine-wide code cache called the global assembly cache. The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer.

You should share assemblies by installing them into the global assembly cache only when you need to. As a general guideline, keep assembly dependencies private, and locate assemblies in the application directory unless sharing an assembly is explicitly required. In addition, it is not necessary to install assemblies into the global assembly cache to make them accessible to COM interop or unmanaged code.

转载于:https://www.cnblogs.com/taoxu0903/archive/2010/09/07/1820412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值