Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Is System.Runtime.InteropServices.GuidAttribute used for anything except COM interop

Writer Sebastian Wright

I'm trying to figure out why this attribute was added to a class. Google is only turning up COM related material when I search for it; but the application doesn't do anything via COM.

The checkin comment doesn't provide any enlightenment (it referred to the other, seemingly non-related changes made at the same time); nor did reading my email traffic for several days on either side of the change.

1

2 Answers

A type in .NET always has a Guid whether you use the attribute or not. Available through the Type.GUID property. The CLR auto-generates one from the type definition, ensuring that identical types have identical Guids regardless on what machine it gets generated. Note that this is very different behavior from the usual way a Guid is generated.

You only use the [Guid] attribute if you want to override the auto-generated guid. Which is only useful in COM interop scenarios to get a declaration to match an existing COM interface or coclass. It should always be near a [ComVisible] or [ComImport] attribute.

The Guid attribute was introduced for COM interop - but there's nothing preventing you (or any other third party) from repurposing it for other uses.

Attributes, generally, provide additional information ("metadata") that can be used by other code however suits.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.