/// <summary> Determines equality by name, containing type, arity and namespace. </summary> /// <param name="x">The first instance.</param> /// <param name="y">The other instance.</param> /// <returns>True if the instances are found equal.</returns> public static bool Equal(ITypeSymbol?x, ITypeSymbol?y) { if (ReferenceEquals(x, y)) { return(true); } if (x is null || y is null) { return(false); } return(x.MetadataName == y.MetadataName && NamespaceSymbolComparer.Equal(x.ContainingNamespace, y.ContainingNamespace) && NullableAnnotation() && TypeArguments()); bool NullableAnnotation() { if (!x.IsReferenceType) { return(true); } return((x.NullableAnnotation, y.NullableAnnotation) switch { (Microsoft.CodeAnalysis.NullableAnnotation.None, _) => true, (_, Microsoft.CodeAnalysis.NullableAnnotation.None) => true, var(xa, ya) => xa == ya, }); }
/// <summary> Determines equality by delegating to other compare. </summary> /// <param name="x">The first instance.</param> /// <param name="y">The other instance.</param> /// <returns>True if the instances are found equal.</returns> public static bool Equal(ISymbol?x, ISymbol?y) { if (ReferenceEquals(x, y)) { return(true); } if (x is null || y is null || x.Kind != y.Kind || x.MetadataName != y.MetadataName) { return(false); } return((x.Kind, y.Kind) switch { (SymbolKind.Field, SymbolKind.Field) => FieldSymbolComparer.Equal((IFieldSymbol)x, (IFieldSymbol)y), (SymbolKind.Event, SymbolKind.Event) => EventSymbolComparer.Equal((IEventSymbol)x, (IEventSymbol)y), (SymbolKind.Property, SymbolKind.Property) => PropertySymbolComparer.Equal((IPropertySymbol)x, (IPropertySymbol)y), (SymbolKind.Method, SymbolKind.Method) => MethodSymbolComparer.Equal((IMethodSymbol)x, (IMethodSymbol)y), (SymbolKind.Parameter, SymbolKind.Parameter) => ParameterSymbolComparer.Equal((IParameterSymbol)x, (IParameterSymbol)y), (SymbolKind.Local, SymbolKind.Local) => LocalSymbolComparer.Equal((ILocalSymbol)x, (ILocalSymbol)y), (SymbolKind.NamedType, SymbolKind.NamedType) => NamedTypeSymbolComparer.Equal((INamedTypeSymbol)x, (INamedTypeSymbol)y), (SymbolKind.ArrayType, SymbolKind.ArrayType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y), (SymbolKind.DynamicType, SymbolKind.DynamicType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y), (SymbolKind.ErrorType, SymbolKind.ErrorType) => false, (SymbolKind.PointerType, SymbolKind.PointerType) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y), (SymbolKind.TypeParameter, SymbolKind.TypeParameter) => TypeSymbolComparer.Equal((ITypeSymbol)x, (ITypeSymbol)y), (SymbolKind.Namespace, SymbolKind.Namespace) => NamespaceSymbolComparer.Equal((INamespaceSymbol)x, (INamespaceSymbol)y), (_, _) => SymbolEqualityComparer.Default.Equals(x, y), });