Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
when working with multiple files the entry point has to be provided
Target type
Multiple Module Assembly
There is a specific order that should be followed when creating a
multimodule assembly.
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 {
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)
}
}
}
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