Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GUID attribute needed in the first place?

What is the necessity for the GUID attribute? why don't just let the compiler handle this automatically?!

like image 849
Nissim Avatar asked Jul 01 '09 06:07

Nissim


2 Answers

If the compiler handled this automatically, you'd end up with one of two situations.

  • A new GUID every time you compiled - since GUIDs are supposed to be published, this would fail.

  • Collisions - if the GUID was the same every time, based on (say) a Hash of the name, multiple projects would end up using the same GUID for different purposes.

The existing approach - an explicit GUID gives developers the power to control these as required.

like image 164
Bevan Avatar answered Sep 22 '22 06:09

Bevan


These are attributes that matter a great deal to COM. Which was the predecessor of .NET and had its heyday in the nineties, before Java stole the show. .NET needed to be compatible with COM to have a chance of succeeding. Or in other words, you needed to be able to write a COM server in a .NET language that a large legacy program could use.

The [ComVisible] attribute ensures that a COM client program can see and use the IEnumerable interface. Essential to allow the client program to enumerate .NET collections.

The [Guid] attribute is crucial in COM, it identifies an interface. Which is done by a guid, not a name, to ensure that it is unique across multiple applications written by different programmers. .NET has this too, but however uses a name to make it easier on humans. "System.Collections.IEnumerable, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

IEnumerable<>, the generic version, doesn't have a [Guid]. Generics are not compatible with COM. It doesn't much matter these days, not much visible COM around anymore, most of it has been wrapped by friendly .NET classes. But still very core in Windows, notably in the brand-new WinRT (aka Metro, aka Modern UI, aka UWP). You don't use that directly either, making COM somewhat like the assembly language of Windows programming.

like image 44
Hans Passant Avatar answered Sep 24 '22 06:09

Hans Passant