C# 学习笔记 (Assembly)-01

NAMESPACE

Simply convenient means of semantically grouping elements - classes and other namespaces - to avoid name collision.

can be nested to any desired level.

in an enviroment in which your names of your classes might duplicate others.

Main

must be defined public and static

Common Type System

using Directive

can't be used to class, but can create the alias for classes. using alias = class; Such as: using output = System.Console;

If the compiler can not understand anything you typed, it will search the listed namespace to find the definition.

Class Ambiguity

PE( Portable Executable )

process: compile source code into binary, the operating system loader loads the binary into memory and "fix-up" any necessary addresses.

.net features:

  1. All .net language have access to same functionality.
  2. Code written in these various language can interoperat seamlessly.
  • Source Code ( a programmer writes )-----------------Compile(_CorExeMain)
  • standard PE ( MSIL and manifest )-----------------Load
  • Entry Point inside PE
  • _CorExeMain ( Mscoree.dll )-----------------JITter( Just in time compiler )
  • Native CPU instruction

In addition to the default JITter

Install-time code generation: Compilation is done in the install time; Compile entire assembly just once before you can run it.

EconoJit ( code pitching ): to discard the generated or compiled code if the system begins to run out of memory, and the code must be recompiled as thougth it had never be called.

dumpin

10 B magic #   represents the field is indeed a valid PE

RVA ( Relative Virtual Address ) Application's Entry Point

ILDASM

three blocks of information in manifest

  1. assembly record: be used to reference an external assembly
  2. Information about the assembly: Attribute enable you to attach textual annotations to a piece of code that define various runtime characteristics for the code.
  3. module record

constructor:

specialname and rtspecialname Attributes

cil managed Attribute: Mark a method is a managed method, managed code is the code can run in the .net framework.

ldarg.0 --- Push the first argument of a method.

call ---- Call a method.

ret. --- Return control from current method to caller.

ldstr --- push literal string.

CSC:

/t:library    a dll with manifest( describe the assembly in .net runtime ); can not be added to another assembly.

/t:module    a dll without manifest

/r:    switch is used only to reference external assemblies.

/addmodule:    switch is used to tell the compiler which modules to add to the assembly that's being created.

Assembly

Singlefile Assembly:  manifest in incorporated into the resulting PE.

Multifile Assembly:  manifest exists as either a standalone entity with the assembly or as an attachment to one of modules with the assembly.

Benifits of assembly

Packaging: runtime need to load the only required assembly

Deployment: Assembly is the smallest unit of Deployment; Assembly is self-describing.

Version: contain the version information of the assembly as well as a list of all referenced assemblies and the version information for them.

  • Dll Hell is referred to a situation in which one application overwrite a dll needed by another application.

public make the class availabe to other code - even the code outside the current assembly

internal access modifier specifies that the type being modified is accessible on to the code in the same assembly.

Create multifile modules and place them into the same accembly so that we can use internal modifier to prevent outside code from accessing types.

Four reasons for using a Strong Name

  1. It's a mechanism in .Net for generating a globally unique name;
  2. key pair includes a signature;
  3. guarantee the third part cannot release a subsequent version of assembly;
  4. runtime can verify the assembly come from published that the caller is expecting

-k switch with the name of output file

[Assembly: AssemblyKeyFile("")]    the constructor enable you to specify a key pair you can use to give your assembly a strong name.

Global Assembly Cache 

three primary purposes

    1. store code download from the internet or other servers; code download for particular application is stored in private portion of the cache;
    2. a data store for components shared by multiple .Net applications; Assembly installed into the cache by Global Assembly Cache tools are stored in the global portion of the cache and accessible by all applications on the machine;
    3. native code version of the assemblies that have been JITted are stored in the cache;

Global Assembly Cache tools: gacutil.exe

  • -i  install
  • -Uninstall, if don't specify the version of the assembly, it will remover all the assemblies with the specified name
  • -l  list

你可能感兴趣的:(C#,1.1)