-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathComExtensions.cs
More file actions
76 lines (63 loc) · 3.33 KB
/
Copy pathComExtensions.cs
File metadata and controls
76 lines (63 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using WindowsRuntime.Generator;
using WindowsRuntime.InteropGenerator.References;
namespace WindowsRuntime.InteropGenerator;
/// <summary>
/// Extensions for COM types.
/// </summary>
internal static class ComExtensions
{
extension(IHasCustomAttribute type)
{
/// <summary>
/// Checks whether a <see cref="IHasCustomAttribute"/> represents a <see cref="System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute"/> type.
/// </summary>
/// <returns>Whether the type represents a <see cref="System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute"/> type.</returns>
public bool IsGeneratedComInterfaceType => type.HasCustomAttribute("System.Runtime.InteropServices.Marshalling"u8, "GeneratedComInterfaceAttribute"u8);
}
extension(TypeDefinition type)
{
/// <summary>
/// Tries to get the generated <c>InterfaceInformation</c> type for a given <see cref="System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute"/> type.
/// </summary>
/// <param name="interopReferences">The <see cref="InteropReferences"/> instance to use.</param>
/// <param name="interfaceInformationType">The resulting <c>InterfaceInformation</c> type.</param>
/// <returns>Whether <paramref name="interfaceInformationType"/> was successfully retrieved.</returns>
public bool TryGetInterfaceInformationType(InteropReferences interopReferences, [NotNullWhen(true)] out TypeSignature? interfaceInformationType)
{
GenericInstanceTypeSignature? unknownDerivedAttributeType = null;
// First, try to find the constructed '[IUnknownDerived<,>]' attribute
for (int i = 0; i < type.CustomAttributes.Count; i++)
{
CustomAttribute currentAttribute = type.CustomAttributes[i];
// We expect the attribute to be a constructed generic one, so we need that type signature
if (currentAttribute.Type?.ToReferenceTypeSignature() is not GenericInstanceTypeSignature typeSignature)
{
continue;
}
// Check that the attribute type is a match
if (!SignatureComparer.IgnoreVersion.Equals(typeSignature.GenericType, interopReferences.IUnknownDerivedAttribute2))
{
continue;
}
// We found the '[IUnknownDerived<,>]' attribute, we can stop and inspect it
unknownDerivedAttributeType = typeSignature;
break;
}
// If we didn't find the '[IUnknownDerived<,>]' attribute, we must stop here. We mostly expect
// this to happen if the generation failed (e.g. the interface declaration was not valid).
if (unknownDerivedAttributeType is null)
{
interfaceInformationType = null;
return false;
}
// The generated 'InterfaceInformation' type is the first generic argument of the attribute
interfaceInformationType = unknownDerivedAttributeType.TypeArguments[0];
return true;
}
}
}