What is the purpose and use of the GuidAttribute?

2

I know the GUID is a 'unique' identifier, but I do not see the why of your application's attribute, as in the example below:

[GuidAttribute("C281C7F1-4AA9-3517-961A-463CFED57E75")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[CLSCompliant(false)]
[TypeLibImportClassAttribute(typeof(System.Threading.Thread))]
[System.Runtime.InteropServices.ComVisible(true)]
public interface _Thread {#
 if !FEATURE_CORECLR
 void GetTypeInfoCount(out uint pcTInfo);

 void GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo);

 void GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId);

 void Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr);#
 endif
}

Font

What is the use and why use GuidAttribute ?

    
asked by anonymous 19.02.2017 / 13:45

1 answer

3

The GUID is a way to create a unique identifier (a identity) that will not change for some object so that it does not depend on any central mechanism that guarantees uniqueness in independent mechanisms.

This object must be globally unique even if this object is used on different machines that do not "know each other."

Think of an internet domain. He is unique. How do you guarantee uniqueness? It has a central organ that does not allow to exist two equal names. It can even delegate this to other organs that have a standard. (CGI.br in Brazil can take care of everything ending with .br).

This works fine because it's something related to the internet, so it only makes sense to have something on the network controlling the guarantee of uniqueness.

If you have an internal network you can have a server that guarantees uniqueness. When we register something in the database, it can guarantee that two registered objects have some unique identifier because it is a central point. Obviously this database can not merge naturally with a similar base on another network since that other database may have generated identifiers that already exists in the first database.

If you do not have a network that allows you to control uniqueness and need to be guaranteed worldwide, no matter what machine the object is placed on, what to do?

An algorithm is created that generates a unique identifier that can not occur again on another machine under normal conditions, so if this object is transmitted or copied to another machine it does not risk collision.

If it is guaranteed that this object will not leave the machine may have something to control centrally. Even so, it will not always be such a fitting mechanism.

Using code

Typically the type needs a unique ID for use with Component Object Model (COM) . Note that you also used the ComVisible attribute. When that is not used in COM the GUID is not necessary unless your application creates this demand for your own reasons.

It is obvious that this type created by Microsoft can be on any machine in the world that has a .NET installed and it needs to be unique, this is the solution.

You may think that since Microsoft did, it could secure the drive more simply. The problem is that any programmer can create a component and could create the same identifier without it knowing. Worse, two or more programmers who do not know each other could create the same identifier. The GUID is the mechanism that avoids these problems.

See how it can be unique .

    
19.02.2017 / 14:16