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

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:47:16

问题


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 someone give me a rundown of what it is, or just point me in the direction of some articles that give more information about this?

Thanks!


回答1:


It is the COM identifier that represents the class in question. The class is designed for COM interop.




回答2:


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.




回答3:


Guid (Globally unique identifier) is used to identify your component by outside world. When you write a project which is going to be used as COM (Component Object Model) you will have to give a unique name. For this reason you need to apply GUID attribute.

You can read more about it here.

GUIDAttributeClass



来源:https://stackoverflow.com/questions/3760126/what-is-the-guid-attribute-that-appears-above-classes-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!