本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。cls - common language specification
the common language specification (cls) aids the development of mixed language programming. it defines a subset of common type system which all class library providers and language designers targeting clr must adhere to.
cls is a subset of cts. if a component written in one language (say c#) is to be used from another language (say vb.net), then the component writer must adhere to types and structures defined by cls. cil - common intermediate language
all compilers complying with cli must generate an intermediate language representation called common intermediate language (cil). the cli uses this intermediate language to either generate native code or use just in time (jit) compilation to execute the intermediate code on the fly.
the microsoft documents refer this standard's implementation as msil (microsoft intermediate language). jit - just in time compiler
the jit or just in time compiler is the part of the runtime execution environment, which is used to convert the intermediate language contained in the executable file, called assemblies, into native executable code.
the security policy settings are referred at this stage to decide if code being compiled needs to be type safe. if not an exception is thrown and jit process is aborted.
ves - virtual execution system
virtual execution system (ves), is more or less equivalent to the jvm (java virtual machine).
ves loads, links and runs the programs written for common language infrastructure contained in portable executable (pe) files.
virtual execution system (ves) fulfills it's loader function by using information contained in the metadata and uses late binding (or linking) to integrate modules compiled separately, which may even be written in different languages.
ves also provides services during execution of the codes, that include automatic memory management, profiling and debugging support, security sandboxes, and interoperability with unmanaged code, such as com components.
managed codes are intermediate language (il) code along with metadata contained in portable executable (pe) files, these may be .exe or .dll. this needs just in time (jit) compiler to convert it into native executable code. there is also a provision of pre compiled executable which is called unmanaged code. the advantage of unmanaged code is that is does not need to jit compilation but has the disadvantage of unportablity across different operating system (os) platforms. microsoft's implementation of cli is clr
the microsoft's implementation and adaptation of the above standard has resulted in difference in terminology, for example common intermediate language (cil) is called microsoft intermediate language (msil) and common language infrastructure (cli) is referred to as common language runtime (clr).
these changes in naming convention, i believe, is to create a branding distinction while implementing the standards. this was probably intended to avoid the clash that occurred with the java the language standard, java the island, java the coffee brand and java the sun's trademark! but, in the long run, it will only lengthen the already long list of confusing acronyms and jargons in the programmer's dictionary.
we use cli and clr interchangeably, however, it will be more correct to say that clr is the microsoft's implementation of cli.
apart from scripted languages like javascript and vbscript, the .net framework presently supports three compiled languages, namely: vb.net, vc++ and c# (pronounced c sharp) these language compilers target this runtime. the type verifiable compiler's output is called managed code.
unsafe codes can also be generated by compilers, which is called unmanaged code. garbage collection is only handled for managed codes.
the managed code has access to common language runtime (clr) features such as multi- language integration, exception handling across language boundaries, security and versioning and a simplified deployment .
an interesting facility being experimented by microsoft is the cross language inheritance. for example, a c# class can inherit from a vb object! each of these features will be discussed in detail later.
the clr provides services to the managed code. the language compilers emit metadata, that describes the types, members, and references in the code. metadata is stored along with the code: every loadable common language runtime image contains metadata.
the metadata helps the clr to locate and load classes, lay out instances in memory, resolve method invocations, generate native code, enforce security, and set up run time context boundaries.
the clr, much like java virtual machine (jvm) provides automatic garbage collection facilities to the managed code, this garbage collection feature is called managed data. but unlike java vm, the clr also has mechanism to syntactically switch off automatic garbage collection called unmanaged data, where the programmer is responsible for garbage collection.
the clr has been designed to facilitate cross language integration. two kind of integration is possible: tightly coupled and loosely coupled, which is also called remoting. the tightly coupled inter language method call is achieved within the clr; this assumes that the two languages calling each other are both .net framework compliant like vc++, vb.net or c# or are at least com compliant. thus c# programs can talk to java programs through activex java bean bridge! this is assuming that both the c# and java codes reside on a single computer.
remoting or loosely coupled inter language interaction is suitable when the two interacting programs written in different languages are on different operating system (os) platforms, like c# client residing on windows ce talking to solaris based server side java code. this integration is achieved through an xml based protocol called simple object access protocol (soap) which was proposed by microsoft and is adopted by w3c consortium (http://www.w3c.org). an open source soap gateway implementation of java is available from apache.org at http://xml.apache.org.
soap has transport layer independent, xml formatted content and currently http and smtp transport implementations are available from both microsoft and apache.org for .net framework and java platforms respectively .
all .net framework components carry information about the components and resources they use, in a xml formatted document called metadata. the runtime, uses this information to dynamically link the components, ensuring version integrity and security controls; this makes the application theoretically more resilient against version changes. only time will tell if this innovation is successfully implemented.
another good feature introduced in this new framework is reduction of windows system registry dependency. registration information and state data are no longer stored in the system registry, but inside the metadata. this should make the server side component deployment much easier.
.net framework's common language runtime (clr) claims to have the ability to compile once and run on any cpu and operating system that supports the runtime. we will see if this becomes a real possibility in near future. common intermediate language (cil)
the .net framework's implementation of common intermediate language (cil) is called microsoft intermediate language (msil). unless specified otherwise, we will use the terms intermediate language (il), msil and cil interchangeably.
managed code is produced by one of the three compilers which translate the source code into microsoft intermediate language (msil).
common intermediate language (cil) and therefore it's microsoft rendering called microsoft intermediate language (msil) is said to be a cpu independent set of instructions that can be efficiently converted to native code.
msil intermediate instruction set has instructions for loading, storing, initializing, object method calling , many conventional instructions for arithmetic and logical operations, control flow, direct memory access, and exception handling. all the three languages included in this framework have java like /"try catch/" exception handling facility.
just like java, before the managed code is executed, the intermediate language is converted to cpu specific code by a just in time (jit) compiler. the runtime supplies one or more jit compilers for each computer architecture it supports. however, the code can be compiled into native form during installation itself.
when a common language specification (cls) compliant compiler produces common intermediate language (cil), it also produces metadata, describing the common language types (clt) specific types used in the code, including the definition of each type, the signatures of each type's members, the members that the code references, and other data that the runtime uses at execution time.
the msil and metadata are contained in a portable executable (pe) file which is an extension of the microsoft portable executable (pe) and unix world's common object file format (coff) used for executable content. they appear to the user as the familiar .exe and .dll files.
one of the fundamental differences between java virtual machine (jvm) instruction sets and common intermediate language (cil) is that jvm is big endian ( most significant byte first) and cil uses little endian ( least significant byte first) binary representation. this difference will not be apparent to most of the programmers. only system level programmers would have to deal with it.
the file format, can accommodate either of common intermediate language or native code as well as metadata, a signature pattern enables the operating system to recognize common language runtime images.
the presence of metadata in the executable file enables the components to be self descriptive. this eliminates the need for additional type libraries or interface definition language (idl) used in dcom and corba. the runtime locates and extracts the metadata from the file as necessary during execution. managed execution
there are two kinds of codes that can exist inside the executable files now, the old machine dependent codes, like existing activex controls, are called unmanaged
as mentioned earlier, there are currently three compiled languages c#, c++ and vb provided by microsoft, which target the common language runtime (clr). this runtime is a multi-language execution environment, and supports a common base of data types and language features. however, the language compiler determines what subset of the runtime's functionality is available, and the design pattern of the code is influenced by the features exposed by the compiler.
the coding syntax is determined by the compiler, not by the runtime. if the component is required to be completely usable by components written in other languages, it must use only language features that are included in the common language specification (cls) in the component's exported types. application domains
application domains are light weight process. it can be visualized as an extension of java's sandbox security and thread model.
the common language runtime provides a secure, lightweight unit of processing called an application domain. application domains also enforce security policy.
by light weight it means that multiple application domains run in a single win32 process, yet they provide a kind of fault isolation, that is fault in one application domain does not corrupt other application domains. this aids in enhancing execution security against viruses as well as helps in debugging faulty codes.
the common language runtime relies on type safety and verifiability features of common type system (cts) to provide fault isolation between application domains. since type verification can be conducted statically before execution, it is cost efficient and needs less security support from microprocessor hardware.
each application can have multiple application domains associated with it. and each application domain has a configuration file, containing security permissions. this configuration information is used by the common language runtime to provide sandbox security similar to that of java sandbox model.
although multiple application domains can run within a process, no direct calls are allowed between methods of objects in different application domains. instead, a proxy mechanism is used for code space isolation.