Assembly.Load("myDll,Version=1.0.0.1,Culture=neutral,PublicKeyToken=9b35aa32c18d4fb1");

 

 

 

Sn –k a.snk

Sn –k b.snk

 

We first use the strong name command sn to create for us two unique public key private key pairs in files a.snk and b.snk.

 

a.cs

public class zzz

{

public static void Main()

{

aaa a = new aaa();

System.Console.WriteLine(a.abc());

}

}

 

b.cs

public class aaa

{

public string abc()

{

return "vijay";

}

}

 

Z.bat

del a.exe

del b.dll

csc /target:library b.cs

csc a.cs /r:b.dll

 

We create a dll b.dll which has one function abc in class aaa which is called in the console application a.cs. We have created a batch file z.bat that automates the whole process. When we run a.bat we see vijay displayed as the output.

 

Now lets change a single line in z.bat as follows

 

csc a.cs /r:b.dll /keyfile:a.snk

 

error CS1577: Assembly generation failed -- Referenced assembly 'b' does not

        have a strong name

 

All that we did was associate the file a.exe with a strong name. By doing this we have made sure that any assembly that it now refers to must also have a strong name. The assembly b.dll has no strong name associated with it and hence we get a error. Thus if we associate a strong name with a exe file it can only code from a dll that has a strong name. Lets now look at the same problem the other way around.

 

z.bat

csc /target:library b.cs /keyfile:b.snk

csc a.cs /r:b.dll

 

We give a strong name to the file b.dll by associating the public key private key stored in b.snk. We get no error as a exe file with no strong name can call other code in a dll that has a strong name. This should e obvious as code in mscorlib.dll is called by us, this dll has a strong name, code that calls it normally does not.

 

z.bat

csc /target:library b.cs /keyfile:b.snk

csc a.cs /r:b.dll /keyfile:a.snk

 

The way out is to make sure that both assemblies a.exe and b.dll have a strong name associated with them to get no error.

 

Z.bat

csc /target:library b.cs

csc a.cs /r:b.dll 

 

We now simply run z.bat as

 

This gives us the exe file a.exe. All our C# code gets converted to a intermediate language or IL. All code written in any .Net language gets converted to IL. We have a tool called ildasm that tells us what is present in our assemblies. We run this tool as

 

Ildasm a.exe /out:a.il

 

This creates a file called a.il and we show you a small part of it.

a.il

.assembly extern b

{

  .ver 0:0:0:0

}

.assembly a

{

  .hash algorithm 0x00008004

  .ver 0:0:0:0

}

 

It shows us that we are referring to the assembly b using the extern keyword and both assembly a and b have no public key. We change z.bat as

 

z.bat

csc /target:library b.cs /keyfile:b.snk

csc a.cs /r:b.dll 

 

a.il

.assembly extern b

{

  .publickeytoken = (AB 8E 30 32 1B 02 45 03 )                         // ..02..E.

  .ver 0:0:0:0

}

.assembly a

{

  .hash algorithm 0x00008004

  .ver 0:0:0:0

}

 

Assembly a has not changed at all, but assembly b has now a public key token. We now run ildasm on b.dll

 

Ildasm b.dll /out:b.il

 

This gives us in assembly b as keyword that is called publickey. This keyword has the same value when we run a program called secutil

 

secutil -hex -strongname b.dll

 

This show us the same public key that we see in the file b.il which is too large to be displayed in our tutorial.

 

sn -Tp b.dll

 

When we run the sn command with the Tp option we see the same public key bit now we also see a public key token which is the same that is displayed for assembly b. We now change our bat file to include the /keyfile option for a.exe and we now see another public key in the file a.il for the assembly a. This is how we add a public key into our assembly.

 

Sn –p a.snk c.snk

 

The –p option takes the public key stored in the file a.snk and copies it to c.snk which is only 160 bytes large. This public key can be viewed in a hex editor and is the same as show by the above programs.