From 281827e0d99cddc1350cc037323b3a5a7a02e93e Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Tue, 7 Dec 2021 16:02:28 -0500 Subject: [PATCH] fix: Uno.Equality ambiguity with internal uno types This workaround can be removed once [this type](https://github.com/unoplatform/uno/blob/574a441497e0e6faee8d389a3dbac1c40d4d7891/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Equality/IKeyEquatable.cs#L26) has been renamed. --- src/Uno.CodeGen/EqualityGenerator.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Uno.CodeGen/EqualityGenerator.cs b/src/Uno.CodeGen/EqualityGenerator.cs index 242a16a..d774d9d 100644 --- a/src/Uno.CodeGen/EqualityGenerator.cs +++ b/src/Uno.CodeGen/EqualityGenerator.cs @@ -131,6 +131,8 @@ namespace Uno _dataAnnonationsKeyAttributeSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.KeyAttribute"); _isPureAttributePresent = context.Compilation.GetTypeByMetadataName("System.Diagnostics.Contracts.Pure") != null; + ApplyWorkaroundForUnoEqualityAssembly(context); + _generateKeyEqualityCode = _iKeyEquatableSymbol != null; foreach (var type in EnumerateEqualityTypesToGenerate()) @@ -139,6 +141,24 @@ namespace Uno } } + private void ApplyWorkaroundForUnoEqualityAssembly(SourceGeneratorContext context) + { + if (_iKeyEquatableSymbol == null && _iKeyEquatableGenericSymbol == null) + { + // Workaround for internal types duplicated in Uno.Foundation and other Uno assemblies + // causing context.Compilation.GetTypeByMetadataName to return null because of the ambiguity. + + if (context.Compilation.ExternalReferences.FirstOrDefault(r => r.Display.EndsWith("Uno.Core.Extensions.Equality.dll", StringComparison.OrdinalIgnoreCase)) is MetadataReference equalityRef) + { + if (context.Compilation.GetAssemblyOrModuleSymbol(equalityRef) is IAssemblySymbol assemblySymbol) + { + _iKeyEquatableSymbol = assemblySymbol.GetTypeByMetadataName("Uno.Equality.IKeyEquatable"); + _iKeyEquatableGenericSymbol = assemblySymbol.GetTypeByMetadataName("Uno.Equality.IKeyEquatable`1"); + } + } + } + } + private void GenerateEquality(INamedTypeSymbol typeSymbol) { var builder = new IndentedStringBuilder();