Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/Authoring/WinRT.SourceGenerator/WinRTTypeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,15 +1574,8 @@ private void AddOverloadAttributeForInterfaceMethods(TypeDeclaration interfaceTy
}
}

private void AddGuidAttribute(EntityHandle parentHandle, string name)
private void AddGuidAttribute(EntityHandle parentHandle, Guid guid)
{
Guid guid;
using (SHA1 sha = new SHA1CryptoServiceProvider())
{
var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name));
guid = Helper.EncodeGuid(hash);
}

var uint32Type = Model.Compilation.GetTypeByMetadataName("System.UInt32");
var uint16Type = Model.Compilation.GetTypeByMetadataName("System.UInt16");
var byteType = Model.Compilation.GetTypeByMetadataName("System.Byte");
Expand Down Expand Up @@ -1620,6 +1613,33 @@ private void AddGuidAttribute(EntityHandle parentHandle, string name)
AddCustomAttributes("Windows.Foundation.Metadata.GuidAttribute", types, arguments, parentHandle);
}

private void AddGuidAttribute(EntityHandle parentHandle, string name)
{
Guid guid;
using (SHA1 sha = new SHA1CryptoServiceProvider())
{
var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name));
guid = Helper.EncodeGuid(hash);
}

AddGuidAttribute(parentHandle, guid);
}

private void AddGuidAttribute(EntityHandle parentHandle, ISymbol symbol)
{
if (symbol.TryGetAttributeWithType(Model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.GuidAttribute"), out AttributeData guidAttribute)
&& guidAttribute is { ConstructorArguments.IsDefaultOrEmpty: false }
&& guidAttribute.ConstructorArguments[0].Value is string guidString
&& Guid.TryParse(guidString, out Guid guid))
{
AddGuidAttribute(parentHandle, guid);
}
else
{
AddGuidAttribute(parentHandle, symbol.ToString());
}
}

private void AddCustomAttributes(
string attributeTypeName,
IList<ITypeSymbol> primitiveTypes,
Expand Down Expand Up @@ -1795,7 +1815,7 @@ public override void VisitDelegateDeclaration(DelegateDeclarationSyntax node)
currentTypeDeclaration.Handle = typeDefinitionHandle;
typeDefinitionMapping[QualifiedName(symbol, true)] = currentTypeDeclaration;

AddGuidAttribute(typeDefinitionHandle, symbol.ToString());
AddGuidAttribute(typeDefinitionHandle, symbol);
}

public void AddEventDeclaration(string eventName, ITypeSymbol eventType, ISymbol symbol, bool isStatic, bool isInterfaceParent, bool isPublic = true)
Expand Down Expand Up @@ -2068,7 +2088,7 @@ void AddComponentType(INamedTypeSymbol type, Action visitTypeDeclaration = null)

if (isInterface)
{
AddGuidAttribute(typeDefinitionHandle, type.ToString());
AddGuidAttribute(typeDefinitionHandle, type);
AddOverloadAttributeForInterfaceMethods(typeDeclaration);
}

Expand Down Expand Up @@ -2791,4 +2811,4 @@ public static string QualifiedName(ISymbol symbol, bool includeGenerics = false)
return QualifiedName(symbol.ContainingNamespace.ToString(), GetGenericName(symbol, includeGenerics));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@
name="AuthoringTest.CustomXamlMetadataProvider"
threadingModel="both"
xmlns="urn:schemas-microsoft-com:winrt.v1" />
<activatableClass
name="AuthoringTest.CustomInterfaceGuidClass"
threadingModel="both"
xmlns="urn:schemas-microsoft-com:winrt.v1" />
</file>
</assembly>
</assembly>
13 changes: 13 additions & 0 deletions src/Tests/AuthoringConsumptionTest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,17 @@ TEST(AuthoringTest, XamlMetadataProvider)
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<Windows::Foundation::TimeSpan>>()), nullptr);
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<BasicEnum>>()), nullptr);
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<FlagsEnum>>()), nullptr);
}

TEST(AuthoringTest, CustomInterfaceGuid)
{
CustomInterfaceGuidClass customInterfaceGuidClass;
winrt::com_ptr<::IUnknown> customInterfaceClassUnknown = customInterfaceGuidClass.as<::IUnknown>();
ICustomInterfaceGuid customInterface;

IID customInterfaceIid;
check_hresult(IIDFromString(L"{26D8EE57-8B1B-46F4-A4F9-8C6DEEEAF53A}", &customInterfaceIid));
check_hresult(customInterfaceClassUnknown->QueryInterface(customInterfaceIid, reinterpret_cast<void**>(winrt::put_abi(customInterface))));

EXPECT_EQ(customInterface.HelloWorld(), L"Hello World!");
}
11 changes: 11 additions & 0 deletions src/Tests/AuthoringTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,17 @@ private static int GetNumberFromAbi(void* thisPtr, int* value)
}
}
}

[System.Runtime.InteropServices.Guid("26D8EE57-8B1B-46F4-A4F9-8C6DEEEAF53A")]
public interface ICustomInterfaceGuid
{
string HelloWorld();
}

public sealed class CustomInterfaceGuidClass : ICustomInterfaceGuid
{
public string HelloWorld() => "Hello World!";
}
}

namespace ABI.AuthoringTest
Expand Down