Web Design that taps into the haromny and vision of your dreams.

Creating and Using a .NET Assembly in C#

How to create and use .Net Assembly

Written By on in C#

588 words, estimated reading time 3 minutes.

Visual Studio provides easy methods for creating and using a .Net assembly. In this tutorial, we look at creating an assembly and how we can use different methods for incorporating it into our projects.

Using Visual Studio Series
  1. Getting Started with Visual Studio
  2. Using the Visual Studio Integrated Development Environment
  3. Using the Visual Studio Debugger
  4. XML Documentation in Visual Studio
  5. Using Microsoft .Net Command Line Tools
  6. What is an Assembly in Microsoft .Net?
  7. Creating and Using a .NET Assembly in C#
  8. Creating and Implementing Satellite Assemblies
  9. Creating Strong Named Assemblies in Microsoft .Net
  10. Visual Studio Task List
  11. Resources and Localisation in C# Projects
  12. Localising Microsoft .Net Applications with C#
  13. Using Resource Files in C# Applications
  14. Using XSD Tool to Generate Classes from XML
  15. 10 Visual Studio Tools You May Not Know About

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 and build the application you will notice that it failed with a message stating that

"The type or namespace name could not be found (are you missing a directive or an assembly reference?)"

We are seeing this message because we haven't told the console application about the class library (even though it is part of the same solution). To do this we need to add a reference to the class library. This can be done by right-clicking on the console application in solution explorer and selecting "Add Reference".

Add reference to projects
Add reference to projects

From this box, you can select an existing.Net assembly or browse to a file or project. Click the Projects tab and select our class library.

Finally, we will need to add the namespace to the using statements or get refactor to do it for us.

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.

Using third party assemblies

To use a third party assembly you can use the same method as above, simply add a reference to the assembly and add in a using clause (or explicitly reference classes and data types).

Creating a Shared Assembly

The procedure for creating a shared assembly is the same as for a private assembly, except that there are a few extra steps involved before you can use it. All shared assemblies need to be strongly named before they can be installed into the global assembly cache. Strongly named assemblies ensure that the assembly has a unique manifest consisting of versioning information and public key. For details on how to strongly name an assembly please see the tutorial Creating Strong Named Assemblies.

Last updated on: Thursday 22nd June 2017

 

Comments
Gaurav Balyan

Gaurav Balyan

very nicely and throughly explained

Reply to Gaurav Balyan
Arjunan

Arjunan

Hi,

Clear Explanation.Thanks for giving this information with simple Examples...

Thanks u very much !!!

Reply to Arjunan
excellent

excellent

excellent

Reply to excellent

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me