File: System\Reflection\Metadata\DataObject.Composition.TypeNameComparer.cs
Web Access
Project: src\src\System.Private.Windows.Core\src\System.Private.Windows.Core.csproj (System.Private.Windows.Core)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
namespace System.Reflection.Metadata;
 
/// <summary>
///  Match <see cref="TypeName"/>s by matching full namespace-qualified type names and full assembly names,
///  including the version.
/// </summary>
internal sealed class TypeNameComparer : IEqualityComparer<TypeName>
{
    private TypeNameComparer()
    {
    }
 
    internal static IEqualityComparer<TypeName> Default { get; } = new TypeNameComparer();
 
    public bool Equals(TypeName? x, TypeName? y)
    {
        if (x is null && y is null)
        {
            return true;
        }
 
        if (x is null || y is null)
        {
            return false;
        }
 
        return x.Matches(y);
    }
 
    public int GetHashCode(TypeName obj)
    {
        if (obj is null)
        {
            return 0;
        }
 
        if (obj.IsArray)
        {
            return true.GetHashCode() ^ obj.GetArrayRank() ^ GetHashCode(obj.GetElementType());
        }
 
        int hashCode;
        if (obj.IsConstructedGenericType)
        {
            hashCode = "constructed".GetHashCode() ^ GetHashCode(obj.GetGenericTypeDefinition());
            foreach (TypeName genericName in obj.GetGenericArguments())
            {
                hashCode ^= GetHashCode(genericName);
            }
 
            return hashCode;
        }
 
        hashCode = obj.FullName.GetHashCode();
        if (obj.AssemblyName is AssemblyNameInfo info)
        {
            hashCode ^= info.Name.GetHashCode();
            if (info.Version is not null)
            {
                hashCode ^= info.Version.GetHashCode();
            }
 
            if (info.CultureName is not null)
            {
                hashCode ^= info.CultureName.GetHashCode();
            }
 
            if (!info.PublicKeyOrToken.IsDefaultOrEmpty)
            {
                foreach (byte b in info.PublicKeyOrToken)
                {
                    hashCode ^= b.GetHashCode();
                }
            }
        }
 
        return hashCode;
    }
}