[Mono-dev] Type.GUID patch

Robert Jordan robertj at gmx.net
Sat Sep 3 11:00:42 EDT 2005


Kornél,

> It is usually required for COM interop. I think NotImplementedException
> should be thrown for type without GuidAttribute instead of Guid.Empty
> because MS.NET really generates a Guid using an algorythm that seems to be
> somehow deterministic.

The automatically generated GUID appears to be definitely a hash of
the assembly and type name. I compiled and ran the folowing test
on 3 machines (2K, XP and 2K3) with different runtimes versions.
The generated GUID was the same.

// filename: gt.cs
// csc gt.cs
using System;
class App
{
     static void Main ()
     {
         Console.WriteLine (typeof (App).GUID);
     }
}

output: 781ab680-c202-36e3-ad73-f4b165ce2d38

The hash appears to change with the assembly name and type name.
Renaming gt.cs will return another GUID as well as renaming
"App". Renaming gt.exe doesn't change the GUID.
Applying an AssemblyVersionAttribute will change the GUID,
so I'm pretty sure, that Type.AssemblyQualifiedName is taken
into account while generating the hash.

The following algorithm computes the GUID from
Type.AssemblyQualifiedName using a MD5 hash:

Guid ComputeGuid (Type t)
{
     byte[] bytes = System.Text.Encoding.UTF8.
         GetBytes (t.AssemblyQualifiedName);
     using (System.Security.Cryptography.MD5 md5 =
            System.Security.Cryptography.MD5.Create ()) {
         return new Guid (md5.ComputeHash (bytes));
     }
}

Is it a patch worth?

Rob


> ----- Original Message -----
> From: "Robert Jordan" <robertj at gmx.net>
> To: <mono-devel-list at lists.ximian.com>
> Sent: Saturday, September 03, 2005 1:58 PM
> Subject: Re: [Mono-dev] Type.GUID patch
> 
> 
>> Jonathan,
>>
>>> Here's a patch for the Type.GUID property. It was previously always
>>> returning Guid.Empty. It now returns the value of the GuidAttribute
>>> specified on the Type, else it returns Guid.Empty. This seems to be the
>>> behavior of .Net. Please review and commit, or give me permission to
>>> commit.
>>
>>
>> BTW, .NET's Type.GUID never returns Guid.Empty. If GuidAttribute
>> isn't applied, it returns a GUID that seems to be somehow
>> derived/generated from the type name at runtime.
>>
>> Rob
>>
>> _______________________________________________
>> Mono-devel-list mailing list
>> Mono-devel-list at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>




More information about the Mono-devel-list mailing list