Tarida George
Tarida George

Reputation: 1303

C# data PublicKeyToken value

If I open Resources.resx from my project C# folder I saw something like this :

<data name="picture" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\picture.png;System.Drawing.Bitmap, System.Drawing, 
        Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

What is PublicKeyToken and what value should I assign to it ?

Upvotes: 5

Views: 2600

Answers (3)

Erdem AGA
Erdem AGA

Reputation: 3

If you look at the properties of this reference you will notice that "Copy Local" has been set to "True" by default.

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66388

The .NET is working with assemblies; every EXE or DLL file is considered an assembly.

In order for assembly to be globally recognized on a machine by any other .NET code who needs it (without copying it locally of course) you can place assemblies in the Global Assembly Cache aka GAC.

You can't place just any assembly in the GAC, for security reasons placing assembly there requires it to be strongly typed aka signed. Won't get here into the process of signing an assembly as it's not relevant, but part of the process of signing is getting a public key. You can then use this same key over any amount of assemblies so assembly might have one key, but one key might be shared by many different assemblies.

When the .NET is loading a strongly typed assembly it must get three parameters:

  1. Assembly name.
  2. Assembly version.
  3. Assembly public key.

With any of those missing or wrong, .NET will throw error that it can't find the assembly.

Now for your last question: given a known aseembly name, how to find its public key token?

Well, all system assemblies prior to 4.0 can be found by Windows Button + R then typing %windir%\assembly and ENTER. You will be presented with the full table of installed assemblies each with name, version and key. You can see there for example that the key posted in the original question here belongs indeed to System.Drawing assembly.

Due to breaking changes in the 4.0 framework, the GAC location for its assemblies has changed to %windir%\Microsoft.NET\assembly\ and it's no longer displayed in a friendly list manner but rather as folders and files: each assembly in the new GAC has its own folder and from its name you can extract the public token. For example System.Web contains the following sub folder:

v4.0_4.0.0.0__b03f5f7f11d50a3a

So it means the key is b03f5f7f11d50a3a and that's what you should place in the .resx file if you have reference to System.Web assembly. (Note it's the same as System.Drawing key, but it's not really relevant here)

Upvotes: 4

user1968030
user1968030

Reputation:

see this

Microsoft solves the "public key bloat" problem by using a hash of the strongly-named assembly's public key. These hashes are referred to as public key tokens, and are the low 8 bytes of the SHA1 hash of the strongly-named assembly's public key. SHA1 hashes are 160 bit (20 byte) hashes, and the top 12 bytes of the hash are simply discarded in this algorithm.

Upvotes: 1

Related Questions