Skip to content

Commit 03a6e09

Browse files
committed
fix(expressions): replace System.Collections.Generic.ReferenceEqualityComparer with hand-rolled comparer
ReferenceEqualityComparer was added in .NET 5 and isn't in UWP's BCL. A tiny IEqualityComparer<ExpressionNode> using ReferenceEquals + the identity hash from RuntimeHelpers gives the same semantics with no framework dependency, so the new UWP CompositionExpressions assembly compiles cleanly.
1 parent 0500e7a commit 03a6e09

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/CompositionExpressions.WinAppSdk/LiveValueOverride.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public static void SetLiveValueProvider(this QuaternionNode node, Func<Quaternio
6868
public static IEnumerable<ScalarNode> EnumerateScalarLeaves(this ExpressionNode root)
6969
{
7070
if (root is null) throw new ArgumentNullException(nameof(root));
71-
var visited = new HashSet<ExpressionNode>(System.Collections.Generic.ReferenceEqualityComparer.Instance);
71+
// Use a hand-rolled reference-identity comparer rather than
72+
// System.Collections.Generic.ReferenceEqualityComparer so this file
73+
// compiles against UWP's older BCL too (the type was added in .NET 5).
74+
var visited = new HashSet<ExpressionNode>(ReferenceComparer.Instance);
7275
var stack = new Stack<ExpressionNode>();
7376
stack.Push(root);
7477
while (stack.Count > 0)
@@ -83,6 +86,13 @@ public static IEnumerable<ScalarNode> EnumerateScalarLeaves(this ExpressionNode
8386
}
8487
}
8588

89+
private sealed class ReferenceComparer : IEqualityComparer<ExpressionNode>
90+
{
91+
public static readonly ReferenceComparer Instance = new();
92+
public bool Equals(ExpressionNode? x, ExpressionNode? y) => ReferenceEquals(x, y);
93+
public int GetHashCode(ExpressionNode obj) => System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
94+
}
95+
8696
/// <summary>
8797
/// Walks the AST rooted at <paramref name="root"/> and yields only
8898
/// <see cref="ScalarNode"/> leaves that look like animatable inputs:

0 commit comments

Comments
 (0)