Você está na página 1de 41

Assemblies

Introduction to Assemblies
• Assemblies are self-describing installation units, consisting of one or
more 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
• 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 where
versioning information for the assembly is stored.
Application Domain
• In .NET environment multiple applications can run in a
single process within multiple application domains.
Executing an assembly from another
assembly using reflection
Demo.cs
using System; output:
namespace n1{ Main in domain Demo.exe
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);
}
}}
ExeDemo.cs
using System; Output:
namespace n1{ in domain ExeDemo.exe
public class Test{ Main in domain Demo
static void Main(){

Console.WriteLine(" in domain
"+AppDomain.CurrentDomain.FriendlyName);

AppDomain s=AppDomain.CreateDomain("Demo");
s.ExecuteAssembly("Demo.exe");
}
}
}
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 case
of multimodule assemblies.
Building assemblies
When working with a single-file assembly, you can create either an
executable assembly or a library assembly.

csc MyAssembly.cs creates exe file with the same name

csc /out:MyNewAssembly.exe MyAssembly.cs

if you want another executable file name

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

csc /main:MyClass MyAssembly.cs


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 library
assembly

csc /out:MyNewAssembly.dll /t:library MyAssembly.cs

Target type
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.
MyMessage.cs

using System;
namespace Messaging
{
class MyMessage
{
public MyMessage(string InMsg)
{
Console.WriteLine("The author says: " + InMsg);
}}}

Compile:
csc /t:module MyMessage.cs
When distributing the application, we must be
SendMsg.cs sure to include the final executable assembly
using System; as well as all compiled modules referenced by
using Messaging; the final assembly. Without these compiled
modules, the executable assembly will
class SendMsg generate a file not found exception.
{
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
Language Independence
C++ Class Library
#pragma once
#include<stdio.h>
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; }
};
}
In the AssemblyInfo.cpp that is generated attribute
[assembly:CLSCompliantAttribute(true)];
for static
method printf

double click on hello1 and


look for printf() and “hello
calling native code” string
Writing VB class
Change the name space to LangIndp
Add the c++ file dll to this project
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
Write C# class
Add reference to HelloVB and HelloCPP

using System;
using System.Collections.Generic; Displays:
hello C++/CLI
using System.Text;
hello from VB
namespace LangIndp hello from c sharp
{
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();
}
}}
GAC( Global Assembly Cache)
• A cache for globally available assemblies
• gacutil /l lists 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
Create a C# lib project
using System;
using System.Collections.Generic;
using System.Text;

namespace Share
{
public class ShareGreet
{
public string greet(string n)
{
return "hello " + n;
}
}
}
Creating a strong name: go to project properties
 signing
build and view the public key token
Loading it in GAC
gacutil /i ShareCSharp.dll (from command line run as admin)

from Visual studio go to project properties build events


Check if it is done
Using shared assembly: create a client
Add reference and Execute
Versioning
• Assemblies have 4 parts in their version
– <Major>.<Minor>.<Build>.<Revision>
– [assembly: AssemblyVersion(“1.0.0.0”)] – in
AssemblyInfo.cs
Getting the versions
using 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.
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
Creating a new version
• Change the greet method to return a different message
• Go to properties and change the version
• 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
To make the client use the older
version
• go to control panel  administrative tools and select
.NET framework Configuration
• Go to application and click ‘add an application to
configure’ and browse and
select the SharedClient.exe
• View Assembly Dependency
• To change the version to make it use old version ,go to
Configure Assemblies right click add
• By disabling the publisher policy, you can configure
different version redirections.

Output:
hello Tom
ShareCSharp, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=5a45dd1c5db98fff

Now run the client application

Você também pode gostar