Assembling assemblies
Posted on 17 Jan 2006 at 16:25
Thomas Lee looks at what .NET assemblies are made up from and shows you how to make them
There are broadly speaking two types of assembly: process assemblies and library assemblies. A process assembly, typically containing EXE files, represents a process that will use classes defined in library assemblies. Unlike Win32, .NET does not employ the filename extension to determine whether the file is a process or library, which means a library may have either a DLL or an EXE extension.
An assembly can be made up of one or more files, which may include multiple code files and a separate manifest. For example, an assembly might contain some executable code plus some resources such as JPEG and BMP image files. Since you can use different languages to create individual code modules under .NET, you could in theory combine several separately compiled code modules to create just one assembly, but in practice this doesn't happen, because Visual Studio only allows you to create an assembly made up of a single code module. If you build a .NET app, such as a console application or Windows app, you must have at least one assembly: your application. Your app may have functionality contained in separate DLLs - for performance reasons, to simplify development or for localisation - and in such cases you may have multiple assemblies to manage.
While I haven't talked about ASP.NET in much detail yet, assemblies are also used there. In the case of ASP.NET, you'll typically have an assembly for each page of your ASP.NET website, plus additional assemblies that perform common logic to be shared among the rest of the pages.
There are two tools you can use to look inside an assembly and view its contents, and it would be good idea to get both of them and start looking inside some assemblies to get acquainted. The first tool is Microsoft's Intermediate Language Dis-assembler, or ILDASM, contained in the .NET Software Development Kit (SDK), which you can download from msdn.microsoft.com. The second program is Reflector, a class browser for .NET components, which you can use to browse and search an assembly's metadata, IL instructions, resources and XML documentation. Written by Lutz Roeder, Reflector is a free download. For more information about the tool and to download a copy, see www.aisto.com
Building Assemblies
Building an assembly is generally done with one of the .NET language compilers, and to illustrate the concept of assemblies and how they work I've created two simple C# programs. The first is called Client (client.exe), which makes use of an external DLL to perform a calculation and print the results. Here's the source code for the client program:
using System;
public class Client
{
public static void Main()
{
Console.WriteLine("In Client.exe");
Maths m = new Maths();
long r1 = 2;
long r2 = 2;
long r = m.Add(r1, r2);
Console.WriteLine ("{0} + {1} = {2}", r1, r2, r);
}
}
The second component, maths.dll, implements the external Maths library use by client.exe, and its source looks like this.
using System;
public class Maths
{
public long Add(long a, long b)
{
Console.WriteLine("In Maths.dll");
return a + b;
}
}
To build these two components, let's use the built-in C# Compiler (of course, to do this you must first have the .NET Framework loaded). Once you've created the two source files client.cs and maths.cs, you compile them from the command line as follows:
advertisement
- Getting to grips with Microsoft's IT Health Environment Scanner
- Virtualise your servers
- The changing face of travel gadgets
- Build your own distributed file system
- The bulletproof Dell that costs an arm and a leg
- Microsoft Office 2010 Technical Preview: Q&A
- Lawnmowers, the TyTN II and one odd insurance request
- There'll never be a bulletproof OS
- How far can we trust apps?
- Five nice touches in Outlook 2010
- Office 2010 Beta – 32-bit or 64-bit – The Choice is Clear
- Why Britain's watchdogs have fewer teeth than goldfish
- Tabbed documents: how to make Office 2010 great
- Outlook 2010 People Pane – does it spell death to Xobni
- Microsoft Outlook 2010 screenshots
- Co-Authoring in Word 2010 and SharePoint Foundation 2010
- Microsoft Outlook 2010 screenshots: Backstage view
- Flash 10.1: Developing for Desktop and Device
- Microsoft Office 2010 screenshots: Recover unsaved items
- Microsoft Word 2010 screenshots: Text Effects
- Microsoft to pay News Corp to stay off Google
- Christmas sales surge knocks out eBay search
- Windows 8 set for 2012 release
- Q&A: Why Conficker was a victim of its own success
- App developers losing faith in Android
- Biz Stone: Murdoch's Google veto will "fail fast"
- Google adds automatic captions to YouTube
- China ramps up cyber spying
- Mozilla maintains dependence on Google
- Windows 7 flying off the shelves
advertisement
Printed from www.pcpro.co.uk


