What is the Guid attribute that appears above classes in C#?

后端 未结 3 1462
南笙
南笙 2021-01-17 08:38

I\'ve picked up some C# code recently and one of the classes has a Guid attribute present above it. I don\'t understand what this is or what it\'s used for.

Can some

3条回答
  •  不知归路
    2021-01-17 09:30

    You might want to take a look at the ComVisibleAttribute class to learn more about the ways you can make managed classes available to unmanaged code.

    The [Guid] is the exact equivalent to the .NET Type.AssemblyQualifiedName. Like

    System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5561934e089
    

    With the obvious distinction that the .NET type name is easier to read by a human. It is necessary to allow a program to discover what DLL needs to be loaded to use a type. In the .NET case, the assemblies are (usually) found by enumerating the GAC. It is file based.

    COM however uses the registry. After that assembly whose source code you looked at gets built and registered then you can find back the [Guid] in the registry. Fire up regedit.exe and navigate to HKLM\Software\Classes\CLSID\{guid}. You'll see the registration key values that the runtime uses to load the CLR and the assembly.

提交回复
热议问题