.NET Assembly using C#

Post date: Mar 10, 2011 10:49:43 AM

Creating Key Pair:

Create a new Pair of Keys using the Strong Name Tool (sn.exe)

 

    sn -k MyKeyPair.snk

 

The content of MyKeyPair.snk file are not human-readable, but you can view the public key with the following command;

 

    sn -tp MyKeyPair.snk

    

This command shows the public key token

 

Windows Cryptographic Serice Provider (CSP)

 

    You can install the newly created key pair into the Window CSP container, which provide a centralized store of key pairs.

 

      sn -i MyKeyPair.snk MyCSPContainer

 

Creating an Assembly Strong Name:

 

    Before you can geneate astrong name, you need tospecify the assembly's version and culture metada; remeber that strong name consists of the assembly name, the version and culture information + public key and digital signature.

 

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Runtime.InteropServices;

 

// General Information about an assembly is controlled through the following

// set of attributes. Change these attribute values to modify the information

// associated with an assembly.

[assembly: AssemblyTitle("Calculator Service")]

[assembly: AssemblyDescription("Calculator Functions")]

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("The GreateIndiaClub ")]

[assembly: AssemblyProduct("ResearchLibrary")]

[assembly: AssemblyCopyright("Copyright ©  2011")]

[assembly: AssemblyTrademark("Pearsltechnology")]

[assembly: AssemblyCulture("")]

 

Specifying the key pair:

 

    To specify a key pair file, add the following attribute to the AssemblyInfo.cs

[assembly: AssemblyKeyFile("MyKeyPair.snk")]

[assembly: AssemblyKeyName("MyCSPContainer")]

Creating the Strong Name

 The .NET language compilers will automatically create the strong name if youspecify AssemblyInfo.cs in the list of files compile.

csc /out:SingleFileAssembly.dll /target:library Calculator.cs SumArray.cs AssemblyInfo.cs

Creating the Strong name for Multifile assembly

    

    For a single-file asembly, the compiler uses the assembly attributes to locate the key pair and generates the strong name.

When working with a multifile assembly, you must specify the information required for the strong name as arguments to the Assembly Linker tool (al.exe)

 

al /out:MultiFileAssembly.dll /target:library /Culture:""/Version:2.1.0 /Keyfile: MyKeyPair.snk SumNumbers.netmodule SumArrary.netmodule

 

we specify the culture and version metadata with the /Culture and /Version arguments.