dotnet / dotnet/dotnet-api-docs

Type.GUID missing GuidAttribute example

Open
#2,802 0 comments 0 reactions 0 assignees View on GitHub
area-System.Runtime.InteropServices doc-enhancement example-request Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

A common use case in writing COM add-ins for Excel is to use ComRegisterFunctionAttribute, and register/un-register the add-in. While the below code sample is non-exemplary and has many dubious practices, it does illustrate a problem I'm stuck with in some code I have to maintain, and my hope is better documentation for `Type.GUID` will maybe save one other engineer some trouble at some point in the future.

Other relevant documentation: [CLSID Key](https://docs.microsoft.com/en-us/windows/win32/com/clsid-key-hklm)

```c#
[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class SynchronousClient
{
#region Just for debugging
public int MyTest(int a, int b)
{
return (a + b);
}
#endregion

#region Excel Addin Registry realted functions
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type));
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type), false);
}

private static string GetSubKeyName(Type type)
{
// (!) This is really bad, because this type's GUID doesn't have [GuidAttribute], so we have know way to know which CLSID in the registry this dll will get registered under.
string s = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable";
return s;
}
#endregion
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.