What is Guidattribute for and why?

Asked

Viewed 237 times

2

I know the GUID is a 'unique' identifier, but I don’t see why your attribute application, 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
}

Source

What is the use and why use the GuidAttribute?

1 answer

3


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

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

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

This works well because it’s something related to the internet, so it only makes sense to have something networked controlling the guarantee of oneness.

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

If you don’t have a network that allows you to control uniqueness and you need to guarantee it all over the world, no matter what machine that object is placed on, what to do?

An algorithm is created that generates a unique identifier that cannot occur again on another machine under normal conditions, so if that object is transmitted or copied to another machine it is not in danger of collision.

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

Using in code

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

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

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

Behold how he manages to be unique.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.