Friday, March 16, 2012

Creating a Private Assembly using C#

Author : Suresh Konjerla

Assembly:

Assembly is a collection of class modules presented as a single DLL or EXE file. Even though EXE files can also be called assemblies, most of the time we talk about libraries if we use this word. Assemblies are very similar to Java Archives (JAR).
Physically, assemblies are files located somewhere on disk and are per definition so-called Portable Executable (PE). Assemblies are loaded on demand. They are not loaded if not needed.
Assemblies have Metadata stored to provide version information along with a complete description of methods and types. Part of this metadata is a Manifest. The manifest includes identification information, public types and a list of other used assemblies.
There are 2 types of assemblies.
1)    Private Assemblies.
2)    Shared Assemblies.

Creating a Private Assembly

We shall first start off with a blank Visual Studio C# solution. Add a new class library to the solution then build and run it. You will notice that although the project built successfully, you cannot run the application. This is because a class library does not have any entry points, unlike a console application. Class libraries are built into assemblies which merely hold classes, code and resources.

In the class library add a method called TestMethod that will show a message box saying "Hello World!” In this example I have changed the default namespace and class name to better reflect the purpose of the assembly.
using System;
using System. Text;
using System.Windows.Forms;
 
namespace TestClassLibrary
{
  public static class TestClass
  {
    public static void TestMethod()
    {
      MessageBox.Show ("Hello World");
    }
  }
}

 If you look inside the bin/Debug folder for the project you will see a .dll file instead of the usual .exe. This is our new assembly.
In order to test our assembly we need to create a project that is capable of running, for example a console application, windows application or asp.net web application. Class libraries can also be used by other class libraries in exactly the same method as I describe below, but then we still need to create a console application to call that class library!
Add a console application project to the current solution and set it as the default start-up project. This will cause the console application to run when we click on play or launch the project. Let's try and use the method we just created:
using System;
using System.Text;
 
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string [] args)
    {
      TestClassLibrary.TestClass.TestMethod();
    }
  }

When we try to build this application, you will notice that it failed with the following error
The type or namespace name could not be found
We will see this issue why because we haven’t told the console Application about class library. To do this go to project menu -> Add Reference
 Select projects tab and Select class library. Form this box you can select any assembly.
 Finally we need to add the namespace statement by using keyword.
using System;
using System.Collections.Generic;
using System.Text;
using TestClassLibrary;
 
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      TestClass.TestMethod();
    }
  }
}

Now when you build the project it should compile and run properly.

No comments:

Post a Comment