File: Internal\Runtime\CompilerServices\MethodNameAndSignature.cs
Web Access
Project: src\src\runtime\src\coreclr\nativeaot\System.Private.CoreLib\src\System.Private.CoreLib.csproj (System.Private.CoreLib)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;

using Internal.Metadata.NativeFormat;

namespace Internal.Runtime.CompilerServices
{
    [CLSCompliant(false)]
    public class MethodNameAndSignature
    {
        public MetadataReader Reader { get; }
        public MethodHandle Handle { get; }

        public MethodNameAndSignature(MetadataReader reader, MethodHandle handle)
        {
            Reader = reader;
            Handle = handle;
        }

        public string GetName()
        {
            Method method = Reader.GetMethod(Handle);
            return Reader.GetString(method.Name);
        }

        public ReadOnlySpan<byte> Name
        {
            get
            {
                Method method = Reader.GetMethod(Handle);
                return Reader.ReadStringAsBytes(method.Name);
            }
        }

        public override bool Equals(object? compare)
        {
            if (compare == null)
                return false;

            MethodNameAndSignature? other = compare as MethodNameAndSignature;
            if (other == null)
                return false;

            // Comparing handles is enough if there's only one metadata blob
            // (Same assumption in GetHashCode below!)
            Debug.Assert(Reader == other.Reader);

            Method thisMethod = Reader.GetMethod(Handle);
            Method otherMethod = other.Reader.GetMethod(other.Handle);

            return thisMethod.Signature.Equals(otherMethod.Signature)
                && thisMethod.Name.Equals(otherMethod.Name);
        }

        public bool ReturnTypeHasInstantiation
        {
            get
            {
                Method method = Reader.GetMethod(Handle);
                Handle returnType = method.Signature.GetMethodSignature(Reader).ReturnType;
                if (returnType.HandleType != HandleType.TypeSpecification)
                    return false;
                Handle inner = returnType.ToTypeSpecificationHandle(Reader).GetTypeSpecification(Reader).Signature;
                return inner.HandleType == HandleType.TypeInstantiationSignature;
            }
        }

        public override int GetHashCode()
        {
            Method method = Reader.GetMethod(Handle);

            // Assumes we only have one metadata blob
            return method.Signature.GetHashCode() ^ method.Name.GetHashCode();
        }
    }
}