15 Assembly

Embed Size (px)

Citation preview

  • 8/14/2019 15 Assembly

    1/41

    Assemblies

  • 8/14/2019 15 Assembly

    2/41

    Introduction to Assemblies

    Assemblies are self-describing installation units, consisting of one ormore files.

    C# assemblies can be EXE, or DLL or a .CAB (cabinet) or a .MSI

    Assemblies can be either shared or private.

    A shared assembly must have a special version number, a unique

    name and usually it is installed in the global assembly cache. Installation of assembly can be as simple as copying its files.

    same assemblies of different versions can be loaded side by side.

    they are self describing : no registry key etc. required . they include

    meta data within themselves.

    A typical assembly will contain

    assembly meta data

    type meta data

    IL code

    Resources

  • 8/14/2019 15 Assembly

    3/41

    The data contained in assembly contains an intermediate form of

    compiled code. This can be disassembled by a MSIL Disassembler

    utility included with the .NET Framework SDK called ildasm

    ildasm ExeDemo.exe

    when manifest is opened we can see version number as well as

    referenced assemblies and their numbers.

    Assemblies contain a manifest. This metadata contains information

    describing the relationship between all of the parts of your assembly

    Assemblies provide versioning. The manifest is also whereversioning information for the assembly is stored.

  • 8/14/2019 15 Assembly

    4/41

    Application Domain In .NET environment multiple applications can run in a

    single process within multiple application domains.

  • 8/14/2019 15 Assembly

    5/41

    Executing an assembly from another

    assembly using reflection

    Demo.cs

    using System;

    namespace n1{

    public class Demo{

    public Demo(int val){

    Console.WriteLine(val+ " in domain

    "+AppDomain.CurrentDomain.FriendlyName);

    }}

    class Test{static void Main(){

    Console.WriteLine("Main in domain

    "+AppDomain.CurrentDomain.FriendlyName);

    }

    }}

    output:

    Main in domain Demo.exe

  • 8/14/2019 15 Assembly

    6/41

    ExeDemo.cs

    using System;

    namespace n1{public class Test{

    static void Main(){

    Console.WriteLine(" in domain

    "+AppDomain.CurrentDomain.FriendlyName);

    AppDomain s=AppDomain.CreateDomain("Demo");

    s.ExecuteAssembly("Demo.exe");

    }}

    }

    Output:

    in domain ExeDemo.exeMain in domain Demo

  • 8/14/2019 15 Assembly

    7/41

    Assembly Manifest

    Important part of assembly is assembly manifest-

    Identity : name, version, culture and public key

    a list of files

    a list of assemblies a set of permissions

    exported types- ExportedTypes refers to the types

    which are exported by the assembly/manifest module,

    but are NOT defined in the manifest module. in caseof multimodule assemblies.

  • 8/14/2019 15 Assembly

    8/41

    Building assemblies

    When working with a single-file assembly, you can create either anexecutable assembly ora library assembly.

    csc MyAssembly.cs creates exe file with the same name

    csc /out:MyNewAssembly.exeMyAssembly.cs

    if you want another executable file name

    when working with multiple files the entry point has to be provided

    csc /main:MyClassMyAssembly.cs

  • 8/14/2019 15 Assembly

    9/41

    Create a library assembly.

    A library assembly is defined as an assembly containing multiple

    classes, but no entry point. The classes within these assemblies

    can be called and used by other assembly. To compile libraryassembly

    csc /out:MyNewAssembly.dll /t:libraryMyAssembly.cs

    Target type

  • 8/14/2019 15 Assembly

    10/41

    Multiple Module Assembly

    There is a specific order that should be followed when creating a

    multimodule assembly.

    1. All code files containing namespaces referenced in other code files

    should be compiled into code modules first.2. All other code files should be compiled into modules.

    3. Use the Assembly Generation Tool to create an output file

    containing the assembly manifest. This file can also act as the

    executable for the application.

  • 8/14/2019 15 Assembly

    11/41

    MyMessage.cs

    using System;

    namespace Messaging

    {

    class MyMessage

    {

    public MyMessage(string InMsg){

    Console.WriteLine("The author says: " + InMsg);

    }}}

    Compile:

    csc /t:module MyMessage.cs

  • 8/14/2019 15 Assembly

    12/41

    SendMsg.cs

    using System;

    using Messaging;

    class SendMsg{

    public static void Main()

    {

    MyMessage MyMsg = new MyMessage("Greetings!");

    }

    }

    Compile

    csc /addmodule:MyMessage.netmodule /t:module SendMsg.cs

    Multifile assembly

    al MyMessage.netmodule SendMsg.netmodule

    /main:SendMsg.Main /out:MainProg.exe /target:exe

    When distributing the application, we must be

    sure to include the final executable assembly

    as well as all compiled modules referenced by

    the final assembly. Without these compiled

    modules, the executable assembly will

    generate a file not found exception.

  • 8/14/2019 15 Assembly

    13/41

    Language Independence

    C++ Class Library

  • 8/14/2019 15 Assembly

    14/41

    #pragma once

    #include

    using namespace System;

    namespace LangIndp {

    public ref class Class1

    {

    public:

    virtual void hello(){

    Console::WriteLine("hello C++/CLI"); }

    virtual void hello1(){

    printf("hello calling native code"); }int add(int i,int j){

    return i+j; }

    };

    }

  • 8/14/2019 15 Assembly

    15/41

    In the AssemblyInfo.cpp that is generated attribute

    [assembly:CLSCompliantAttribute(true)];

  • 8/14/2019 15 Assembly

    16/41

    for staticmethod printf

    double click on hello1 and

    look for printf() and hello

    calling native code string

  • 8/14/2019 15 Assembly

    17/41

    Writing VB class

  • 8/14/2019 15 Assembly

    18/41

    Change the name space to LangIndp

  • 8/14/2019 15 Assembly

    19/41

    Add the c++ file dll to this project

  • 8/14/2019 15 Assembly

    20/41

    Inherit from the Class1 of C++

    Public Class HelloVB

    Inherits Class1

    Public Overrides Sub hello()

    MyBase.hello()

    Console.WriteLine("hello from VB")

    End Sub

    Public Shadows Function Add(ByVal val1 As Integer, ByVal val2 As

    Integer)

    Return val1 + val2

    End Function

    End Class

  • 8/14/2019 15 Assembly

    21/41

    Write C# class

  • 8/14/2019 15 Assembly

    22/41

    Add reference to HelloVB and HelloCPP

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace LangIndp

    {

    class HelloCSharp :HelloVB {

    public override void hello() {

    base.hello();

    Console.WriteLine("hello from c sharp");

    }static void Main(string[] args) {

    HelloCSharp h = new HelloCSharp();

    h.hello();

    }

    }}

    Displays:hello C++/CLI

    hello from VB

    hello from c sharp

  • 8/14/2019 15 Assembly

    23/41

    GAC( Global Assembly Cache)

    A cache for globally available assemblies

    gacutil /llists all the assemblies from the assembly cache

    Shared Assembiles

    must have strong name- 4 parts

    name of assembly version number

    public key

    culture

    compiler automatically adds the public key to the manifest,creates a hash of all files and signs the hash with the private key

    (which is not stored in the assembly. thus it is guaranteed that

    no one can change your assembly

  • 8/14/2019 15 Assembly

    24/41

  • 8/14/2019 15 Assembly

    25/41

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace Share

    {

    public class ShareGreet

    {public string greet(string n)

    {

    return "hello " + n;

    }

    }

    }

  • 8/14/2019 15 Assembly

    26/41

    Creating a strong name: go to project properties

    signing

  • 8/14/2019 15 Assembly

    27/41

    build and view the public key token

  • 8/14/2019 15 Assembly

    28/41

    Loading it in GAC

    gacutil /i ShareCSharp.dll (from command line run as admin)

    from Visual studio go to project properties build events

  • 8/14/2019 15 Assembly

    29/41

    Check if it is done

  • 8/14/2019 15 Assembly

    30/41

    Using shared assembly: create a client

  • 8/14/2019 15 Assembly

    31/41

    Add reference and Execute

  • 8/14/2019 15 Assembly

    32/41

    Versioning

    Assemblies have 4 parts in their version

    ...

    [assembly: AssemblyVersion(1.0.0.0)] in

    AssemblyInfo.cs

  • 8/14/2019 15 Assembly

    33/41

    Getting the versionsusing System;

    using System.Collections.Generic;

    using System.Text;

    using System.Reflection;

    namespace Share{

    public class ShareGreet {

    public string greet(string n) {

    return "hello " + n;

    }

    public string GetAssInfo() {

    return Assembly.GetExecutingAssembly().FullName;}

    }

    }Register the new version of the shared assembly in GAC.

    Build and call this method from the client.

  • 8/14/2019 15 Assembly

    34/41

    namespace SharedClient

    {

    class Program

    {static void Main(string[] args)

    {

    Share.ShareGreet s = new

    Share.ShareGreet();

    Console.WriteLine(s.greet("Tom"));

    Console.WriteLine(s.GetAssInfo());

    }

    }}

    output:

    hello Tom

    ShareCSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5a45dd1c5db98fff

  • 8/14/2019 15 Assembly

    35/41

    Creating a new version

    Change the greet method to return a different message

    Go to properties and change the version

  • 8/14/2019 15 Assembly

    36/41

    Build and register it with GAC

    Rebuild the client and run the client to see which version

    it uses

    Output :

    hello 1Tom

    ShareCSharp, Version=1.1.0.0, Culture=neutral, PublicKeyToken=5a45dd1c5db98fff

  • 8/14/2019 15 Assembly

    37/41

    To make the client use the older

    version go to control panel administrative tools and select

    .NET framework Configuration

  • 8/14/2019 15 Assembly

    38/41

    Go to application and click add an application to

    configure and browse and

    select the SharedClient.exe

    View Assembly Dependency

  • 8/14/2019 15 Assembly

    39/41

    To change the version to make it use old version ,go to

    Configure Assemblies right click add

  • 8/14/2019 15 Assembly

    40/41

  • 8/14/2019 15 Assembly

    41/41

    By disabling the publisher policy, you can configure

    different version redirections.

    N h li li i

    Output:

    hello Tom

    ShareCSharp, Version=1.0.0.0,

    Culture=neutral,

    PublicKeyToken=5a45dd1c5db98fff