Demo on Shared Assemblies

In this example,
Class Library Project: SharedAssemblyLibrary
Console Application Project: SharedAssemblyDemo
1. Create a Class Library Project.
• Open Visual Studio 2016.
• Click on “File” – “New” – “Project”.
• Select the language as “Visual C#” and project template as “Class Library”.
• Enter the name as “SharedAssemblyLibrary”.
• Click on OK. It creates a new class library project. Initially it contains a new class called “class1”.
• In the solution explorer, rename the “class1.cs” as “MySharedMethods.cs”.
• Then type the following code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedAssemblyLibrary
{
public class MySharedMethods
{
public int GetSimpleInterest(int p, int n, int r)
{
int si = p * n * r / 100;
return (si);
}
}
}

2. Create a strong name key.
• Right click on the project in the “Solution Explorer” and choose “Properties”.
• In the project properties, select the check box “Sign the assembly”.
• In the “Choose a strong name key file” drop down, select “” option.
• In the “Create strong name key” dialog box, enter the name of the strong name key file as “MyKeyFile”.
• Uncheck the “Protect my key file with a password” checkbox.
• Click on OK.
assemblies
3. Customize the “Assembly Information” (AssemblyInfo.cs).
• Change the assembly version as “1.2.0.0”.

4. Generate the DLL File.
• Build the class library project by clicking on “Build” menu – “Build Solution”.

5. Write the assembly into GAC (Global Assembly Cache).
• Open the following folder.
C:\Windows\Assembly
• Drag and drop the “SharedAssemblyLibrary.DLL” file from “bin\Debug” folder into the “c:\windows\assembly” folder.

6. Invoke the Shared Assembly.
• Create a new Console Application. Name: SharedAssemblyDemo
• Click on “Project” – “Add Reference”.
• Click on “Browse” tab.
• From the class library project’s “bin\Debug” folder, select the “SharedAssemblyLibrary.dll” file and click on OK. Then the .dll file reference will be added.
assemblies
• Enter the code as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedAssemblyLibrary;
namespace SharedAssemblyDemo
{
class Program
{
static void Main(string[] args)
{
MySharedMethods msm = new MySharedMethods();
int p, n, r;
Console.WriteLine("Enter p, n, r values:");
p = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
r = Convert.ToInt32(Console.ReadLine());
int si = msm.GetSimpleInterest(p, n, r);
Console.WriteLine("Simple Interest is: " + si);
Console.Read();
}
}
}
assemblies