C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 May - - PowerPoint PPT Presentation

c programming in depth
SMART_READER_LITE
LIVE PREVIEW

C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 May - - PowerPoint PPT Presentation

Chair of Softw are Engineering C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 May 2007 Lecture 7: .NET Assemblies, Type Reflection and Attribute-Based Programming Lisa (Ling) Liu How to create and deploy a .NET assembly


slide-1
SLIDE 1

C# Programming in Depth

  • Prof. Dr. Bertrand Meyer

March 2007 – May 2007

Chair of Softw are Engineering

Lecture 7: .NET Assemblies, Type Reflection and Attribute-Based Programming Lisa (Ling) Liu

slide-2
SLIDE 2

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 2

How to create and deploy a .NET assembly library?

  • Build a Vehicle libary, which includes type Car, SportsCar and

MiniVan, UFO and Helicopter as one binary file or several binary files?

  • Application-wide or machine-wide deployment?
  • Deploy the library on local machine or web?
slide-3
SLIDE 3

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 3

How to build extendable applications?

  • Provide some input vehicle to allow the user to specify the module to

plug in

  • Determine if the module supports the correct functionality
  • Obtain a reference to the required infrastructure and invoke the

members to trigger the underlying functionality

slide-4
SLIDE 4

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 4

Overview

Single-file and mutifile assemblies Private and shared assemblies Assembly configuration Type reflection and late binding Attribute-based programming

slide-5
SLIDE 5

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 5

Assembly’s definition

.NET applications are constructed by piecing together any number of assemblies. An assembly (*.exe or *.dll) is a versioned, self- describing binary file hosted by the CLR. An assembly can be composed of multiple modules. A module is a generic term for a valid .NET binary file.

slide-6
SLIDE 6

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 6

The role of .NET assemblies

Promote code reuse

reuse types in a language-independent manner

Establish a type boundary Form versionable units Provide type and composition information (self- describing) Facilitate deployment through configuration files (configrable)

slide-7
SLIDE 7

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 7

Format of a .NET assembly

A Win32 file header A CLR file header CIL code Type metadata An assembly manifest Optional embedded resources

slide-8
SLIDE 8

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 8

Class diagram of the Vehicle.dll assembly

slide-9
SLIDE 9

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 9

Some questions you may ask

Do you want to use the same programming language to implement the libray? Do you want to allow client applications to download the part of the library on demand? Do you want to build the assembly as one binary file or multiple binary files? answer:

  • same language, no, one binary file => Single file assembly
  • different languages, yes, multiple binary files =>

Multifile assembly

slide-10
SLIDE 10

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 10

Single-file and multifile assemblies

A single file assembly is exactly composed of a single module

Contains all of the necessary elements in a single *.exe

  • r *.dll

A multifile assembly is a set of .NET *.dlls that are deployed and versioned as a single logic unit

One of these *.dlls is termed the primary module and

contains the assembly-level manifest.

slide-11
SLIDE 11

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 11

Single-file assemble

Manifest Type Metadata CIL Code (Optional) Resourcses

slide-12
SLIDE 12

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 12

Building and consuming a single-file assembly

You can use command-line compilers or Visual Studio 2005 to create a single-file assembly. By default, the compiler creates an assembly file with an .exe extension Visual studio automatically places a copy of the library assembly into the directory of the client application

slide-13
SLIDE 13

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 13

Multifile assemble

Manifest (References other related files) Type Metadata CIL Code Primary module (*.dll) Type Metadata CIL Code Type Metadata CIL Code *.netmodule *.netmodule *.bmp

slide-14
SLIDE 14

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 14

Building and consuming a mutifile assembly

  • Build a mutifile assembly

1.

Create secondary modules (*.netmodules) csc.exe /t:module *.cs

2.

Create primary module (*.dll) csc.exe /t:library /addmodule:*.netmodule /out:*.dll *.cs

  • Consume a mutifile assemble
  • Supply onle the primary module to the compiler

csc /r: primary.dll client.cs

slide-15
SLIDE 15

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 15

Benefits of multifile assemblies

Provide a very efficient way to download content Enable modules to be authored using multiple .NET programming languages

slide-16
SLIDE 16

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 16

Deploy .NET assemblies

Private assemblies (Xcopy deployment)

Private assemblies are required to be located in the

same directory as the client application

Used to create an application-wide class library

Shared Assemblies

A single copy of a shared assembly can be used by

several applications on a single machine

Used to create machine-wide class library

slide-17
SLIDE 17

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 17

Understanding private assemblies

Install a private assembly

The identity of a private assembly The probing process Configuration of private assemblies

Uninstall a private assembly

Delete the application folder

slide-18
SLIDE 18

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 18

Identify a private assembly

The full identity of a private assembly is composed of

The friendly name of the assembly The numerical version of the assembly

The assembly manifest record the identity of the private assembly

.assembly Vehicle { ... .ver 1:0:0:0}

slide-19
SLIDE 19

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 19

Probe a private assemble

Probing is the process of mapping an external assembly request to the location of the requested binary file Implicit probing Explicit probing

slide-20
SLIDE 20

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 20

Configure private assemblies

<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="lib"/> </assemblyBinding> </runtime> </configuration>

  • Instruct CLR to probe the subdirectories within client application

directory

  • Has the same name as the launching application and take a *.config

file extension

  • Must be deployed in the application directory
  • Use the privatePath attribute to set the probing subdirectory

*.exe.config

slide-21
SLIDE 21

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 21

Understanding shared assemblies

Install a shared assembly

Strongly name a shared assembly Install the shared assembly into the Global Assembly

Cache (GAC) Uninstall a shared assembly

Use command-line utility gacutil.exe to uninstall a

shared assembly

slide-22
SLIDE 22

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 22

Strong name

A strong name is a unique identifier of an assembly A strong name is based on two cryptographically related keys (public key and private key). A strong name consists of following data:

The friendly name of the assembly The version number of the assembly (assigned using

the [AssemblyVersion] attribute)

The public key value (assigned using the

[AssemblyKeyFile] attribute)

An optional culture identifier value for localization

purpose (assigned using the [AssemblyCulture] attribute)

An embedded digital signature created using a hash of

the assembly’s contents and the private key value

slide-23
SLIDE 23

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 23

Generate digital signature at compile-time

*.dll Manifest (with public key) Type Metadata CIL Code digital signature Assembly Hash Code Private Key Data Digital Signature + =

slide-24
SLIDE 24

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 24

Strongly naming an assembly

1. Create the required key data using sn.exe

  • sn –k MyTestKey.snk
  • 2. Inform the C# compiler where the MyTestKey.snk is

located

  • [Assembly:

AssemblyKeyFile(@”C:\MyTestKey\MyTestKey.snk”)]

  • 3. Specify the version of a shared assembly
  • A .NET version is composed of the four parts:

<major>.<minor>.<build>.<version>

  • [Assembly: AssemblyVersion(“1.0.*”)]
slide-25
SLIDE 25

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 25

Install a shared assembly to the GAC

Global Assembly Cache (GAC) is located under the Assembly subdirectory (C:\Windows\Assembly) under your windows directory Install a shared assembly to the GAC by dragging or dropping the assembly to C:\Windows\Assembly

slide-26
SLIDE 26

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 26

gacutil.exe

/i Install a strongly name assembly into the GAC /u Uninstall an assembly from the GAC /l Displays the assemblies (or a specific assembly) in the GAC

slide-27
SLIDE 27

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 27

Delayed signing

Install an assembly into the GAC for testing purpose The administrator or some trusted individual that holds the *.snk file extracs the public key value from the *.snk file sn.exe –p myKey.snk testPublicKey.snk Distribute the extracted testPublicKey.snk to developers The developer specifies the delayed signing attribute in the assembly [Assembly: AssemblyDelaySign(true)] [Assembly: AssemblyKeyFile(@”C:\MyKey\testPublicKey.snk”)] Disable the signature verification process when deploying an assembly to GAC sn.exe –Vr MyAssembly.dll

slide-28
SLIDE 28

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 28

After delayed signing ...

Ship the assembly to the trusted individual who holds the “true” public/private key file The trusted individual Resigns the assembly to provide the correct digital signature sn.exe –R MyAssembly.dll C:\MyKey\myKey.snk Enable the signature verification procedure sn.exe –Vu MyAssembly.dll

slide-29
SLIDE 29

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 29

Consuming a shared assembly

In Visual Studio 2005, you must reference shared assemblies by navigating to the project’s \Bin\Debug directory The Copy Local property can force a copy of a strongly named code library

slide-30
SLIDE 30

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 30

Configure shared assemblies

Build ClientApplication.exe.config file to instruct CLR to bind to the assembly with required version number Set bindingRedirect attribute of dependentAssemble element to the required version number Dynamic updating library

slide-31
SLIDE 31

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 31

Publisher policy assemblies

With Publisher policy assemblies, client applications need not to set specific *.config files CLR performs the request redirection at the level of GAC Create publisher policy assemblies using al.exe command al.exe /link: CarLibraryPolicy.xml /out:policy.1.0.CarLibrary.dll /keyf:C:\MyKey\MyKey.snk /v:1.0.0.0 Disable publisher policy

Build specific client application configuration file and

set the apply attribute of element publisherPolicy as “no”

slide-32
SLIDE 32

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 32

< codeBase> element

The <codeBase> element is used to instruct the CLR to probe for dependent assemblies located at arbitrary locations It can be used to download asseblies from a given URL

slide-33
SLIDE 33

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 33

The System.Configuration namespace

The System.Configuration namespace allows programmers to programmatically read the data within a client configuration file

slide-34
SLIDE 34

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 34

The machine configuration file

machine.config file

It contains machinewide application settings (via an

<appsetting> element)

.NET platform maintains a separate *.config file for

each version of the framework installed on the local machine

slide-35
SLIDE 35
slide-36
SLIDE 36

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 36

How to build extendable applications?

  • Provide some input vehicle to allow the user to specify the module to

plug in (Dynamic loading)

  • Determine if the module supports the correct functionality (Type

reflection)

  • Obtain a reference to the required infrastructure and invoke the

members to trigger the underlying functionality (Late binding)

slide-37
SLIDE 37

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 37

Type reflection

In the .NET universe, reflection is the process of runtime type discovery System.Reflection namespace

Assembly, AssemblyName, EventInfo, FieldInfo,

MethodInfo, ParameterInfo and PropertyInfo System.Type class

GetConstructors(), GetEvents(), GetFields(),

GetInterfaces(), GetMembers(), GetMethods(), GetProperties(), InvokeMember()

slide-38
SLIDE 38

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 38

Reflect the type information

Obtain a type reference using System.Type.GetType() Type t = Type.GetType (typeName); Reflecting on methods MethodInfo[] mi = t.GetMethods(); Reflecting on Fields and properties FieldInfo[] fi = t.GetFields(); PropertyInfo[] pi = t.GetProperties();

How to reflect the type defined in an assembly not known at compile time?

slide-39
SLIDE 39

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 39

Dynamically loading assemblies

Dynamic load The act of loading external assemblies on demand Use methods Load() or LoadFrom() defined in class Assembly to dynamically load an assembly (private or shared assembly)

slide-40
SLIDE 40

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 40

Reflect on shared assemblies

To load a shared assembly from the GAC, the Assembly.Load() must specify a publickeytoken value Two ways of specifying a publickeytoken when loading a shared assembly Assembly a = Assembly.Load(@”System.Windows.Forms, PublicKeyToken=b77a5c561934e089”) AssembleyName asmName; asmName = new AssemblyName(); asmName.SetPublicToken(byte[]);

slide-41
SLIDE 41

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 41

Late binding

Late binding is a technique in which you are able to create an instance of a given type and invoke its members at runtime without having compile-time knowledge of its existence

Assembly a = Assembly.Load(“CarLibrary”); Type miniVan = a.GetType (“CarLibrary.MiniVan”);

  • bject obj = Activator.CreateInstance(miniVan);

MethodInfo mi = miniVan.GetMethod(“TurboBoost”); mi.Invoke(obj, null);

slide-42
SLIDE 42

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 42

Attributed programming

Using attributes, programmers can embed additional metadata into an assembly .NET attributes are class types that extend the abstract System.Attribute base class Attribute consumers

C# compiler Numerous methods in the .NET base class libraries,

for example, Serialize() An attribute is only applied to the “very next” item

slide-43
SLIDE 43

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 43

Predefined attributes in C#

[CLSCompliant] [DllImport] [Obsolete] [Serializable] [NonSerialized] [WebMethod]

slide-44
SLIDE 44

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 44

Building custom attributes

public sealed class VehicleDescriptionAttribute: System.Attribute { private string msgData; public VehicleDescriptionAttribute (string description) {msgData = description;} public vehicleDescriptionAttribute() {} public string Description { get {return msgData;} set {msgData = value;} } }

slide-45
SLIDE 45

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 45

Assembly-level attributes

Apply attributes on all types within a given module or all modules within a given assembly using the [module:] and [assembly:] [assembly: System.CLSCompliantAttribute(true)] Visual studio 2005 AssemblyInfo.cs file A handy place to put attributes that are to be applied at the assembly level

slide-46
SLIDE 46

C# programming lecture 7: .NET assembly, type reflection and attribute-based programming 46

Questions?