Presentation on theme: "Lecture Roger Sutton CO530 Automation Tools 5: Class Libraries and Assemblies 1."— Presentation transcript:
Lecture Roger Sutton firstname.lastname@example.org CO530 Automation Tools 5: Class Libraries and Assemblies 1
CO530 Automation Tools Introduction To use.Net components effectively, it is necessary to be familiar with the essentials of.NET as a component technology and deployment platform. Here concepts such as Common Language Runtime (CLR) Assembly, and Metadata are introduced. In particular the composition of client and class library assemblies will be reviewed, and examples presented to demonstrate some component-oriented principles outlined previously. 2
CO530 Automation Tools Common Language Runtime (CLR) The.NET CLR provides a context in which all.NET components execute irrespective of implementation. The CLR is based on a strict type system to which all.NET languages must adhere – all classes, interfaces, structures, and primitive types in every.NET language must compile to CLR-compatible types. This places some restrictions on the languages that might be used ( essentially those available in the.NET Framework and Visual Studio although third party vendors are seeking to extend this) The CLR manages every runtime aspect of the code, providing memory management, security and access to the operating system services; accordingly such code is referred to as managed code. 3
CO530 Automation Tools Compilation Compiling this managed code is a two stage process: 1.The high-level code is compiled into a language called intermediate language (IL) that is packaged in either a DLL or an EXE. 2.Since a machine’s CPU can execute only native machine code (Not IL), a Just-in-Time (JIT) compiler converts IL into actual machine code at run-time. When high-level code is first compiled, a machine code stub is created for every class method. The stub calls into the JIT compiler the method address as a parameter. The JIT compiler retrieves the corresponding IL from the DLL or EXE, compiles it into machine code and replaces the stub in memory with newly generated machine code. Consequently a method is only converted into machine code once and methods that are never called are never compiled. 4
CO530 Automation Tools.NET Assemblies Past attempts at component technologies (raw DLLs providing functions and COM components) has drawn attention to the need to separate logical attributes shared by a set of components, e.g. version, security, and deployment from the physical file that contains the component. The.NET assembly is a single deployment, versioning and security unit that assembles multiple physical files into a single logical unit; as either a class library (DLL) – a library assembly, or a standalone application (EXE) called an application assembly. Accordingly an assembly might comprise a single DLL containing several components, or several DLLs containing one or more components, or an application providing several components. 5
CO530 Automation Tools Assemblies and Referencing Visual Studio (VS) 2008 has a template called Class Library that is used to create a server-side assembly. This generates a single DLL class library assembly to which binary components can be added simply by declaring the class in the source file. 6 Any client may use a component by importing the definitions of the types and components in the server assembly into the client assembly. This process is called adding a reference, i.e. Project | Add Reference. Five sources are available:.NET class libraries, COM objects registered on the machine, etc.
CO530 Automation Tools Reference paths When a reference is added to an assembly, its name and location are remembered by VS. During compilation VS uses this path to find the assembly with a matching name and imports the type definitions. Alternatively this might be overridden by specifying an alternative reference location in the project properties. Click Reference Paths… 7
CO530 Automation Tools Reference paths – cont’d Paths to other folders containing assemblies can be added: Commencing at the top, VS attempts to locate as many reference assemblies as possible in the folders specified in the order they appear. If unsuccessful, it uses the original location specified when the reference was added. 8 Once references have been added, the pane allows references to be edited, deleted or the order in which they appear changed.
CO530 Automation Tools Assembly Metadata In adding a reference, a client intends to use a binary component. However, this poses the classic component-oriented programming problem – that of type discovery; how is the compiler to know which methods are public, which are private and what the signatures are?.NET solves this using metadata. Metadata is a comprehensive, standard, mandatory and complete way of describing the contents of an assembly. It describes the available types (classes, interfaces, etc.) their containing namespaces, the name of each type, its visibility, its base class, the interface it supports, methods, the methods’ parameters, etc. The assembly metadata is generated automatically by the high-level compiler directly from the source files. The compiler embeds the metadata in the physical file containing the IL (DLL or EXE) Metadata is an essential element for.NET to be component technology and development platform. 9
CO530 Automation Tools The Assembly Manifest As metadata describes the types in an assembly, the manifest describes the assembly itself, providing logical attributes shared by all the modules and components in the assembly, including: The assembly name The version number Its location Security properties, etc. Like metadata, the manifest is generated automatically by the high-level compiler directly from the source files. The manifest is the way.NET captures information about referenced assemblies. This is crucial to ensure version compatibility and to ensure that the assembly gets to interact with the exact trusted set of other assemblies expected. 10