Exemple #1
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(byte[] image, MetadataReader mdReader, uint rid, int entryPointId, GenericElementTypes[] instanceArgs, uint[] tok)
        {
            Token = _mdtMethodDef | rid;
            EntryPointRuntimeFunctionId = entryPointId;

            _mdReader        = mdReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);

            _methodDef = mdReader.GetMethodDefinition(methodDefHandle);
            Name       = mdReader.GetString(_methodDef.Name);
            BlobReader signatureReader = mdReader.GetBlobReader(_methodDef.Signature);

            TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();
            TypeDefinition       declaringTypeDef;

            while (!declaringTypeHandle.IsNil)
            {
                declaringTypeDef    = mdReader.GetTypeDefinition(declaringTypeHandle);
                DeclaringType       = mdReader.GetString(declaringTypeDef.Name) + "." + DeclaringType;
                declaringTypeHandle = declaringTypeDef.GetDeclaringType();
            }

            NamespaceDefinitionHandle namespaceHandle = declaringTypeDef.NamespaceDefinition;

            while (!namespaceHandle.IsNil)
            {
                NamespaceDefinition namespaceDef = mdReader.GetNamespaceDefinition(namespaceHandle);
                DeclaringType   = mdReader.GetString(namespaceDef.Name) + "." + DeclaringType;
                namespaceHandle = namespaceDef.Parent;
            }

            SignatureHeader signatureHeader = signatureReader.ReadSignatureHeader();

            IsGeneric = signatureHeader.IsGeneric;
            GenericParameterHandleCollection genericParams = _methodDef.GetGenericParameters();

            _genericParamInstanceMap = new Dictionary <string, GenericInstance>();

            int argCount = signatureReader.ReadCompressedInteger();

            if (IsGeneric)
            {
                argCount = signatureReader.ReadCompressedInteger();
            }

            ReturnType = new SignatureType(ref signatureReader, mdReader, genericParams);
            ArgTypes   = new SignatureType[argCount];
            for (int i = 0; i < argCount; i++)
            {
                ArgTypes[i] = new SignatureType(ref signatureReader, mdReader, genericParams);
            }

            if (IsGeneric && instanceArgs != null && tok != null)
            {
                InitGenericInstances(genericParams, instanceArgs, tok);
            }
        }
Exemple #2
0
        private void InitGenericInstances(GenericParameterHandleCollection genericParams, GenericElementTypes[] instanceArgs, uint[] tok)
        {
            if (instanceArgs.Length != genericParams.Count || tok.Length != genericParams.Count)
            {
                throw new BadImageFormatException("Generic param indices out of bounds");
            }

            for (int i = 0; i < genericParams.Count; i++)
            {
                var key = _mdReader.GetString(_mdReader.GetGenericParameter(genericParams.ElementAt(i)).Name);

                if (instanceArgs[i] == GenericElementTypes.ValueType)
                {
                    string classname = _mdReader.GetString(_mdReader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle((int)tok[i])).Name);
                    _genericParamInstanceMap[key] = new GenericInstance(instanceArgs[i], classname);
                }
                else
                {
                    _genericParamInstanceMap[key] = new GenericInstance(instanceArgs[i], Enum.GetName(typeof(GenericElementTypes), instanceArgs[i]));
                }
            }

            if ((ReturnType.Flags & SignatureType.SignatureTypeFlags.GENERIC) != 0)
            {
                ReturnType.GenericInstance = _genericParamInstanceMap[ReturnType.TypeName];
            }

            for (int i = 0; i < ArgTypes.Length; i++)
            {
                if ((ArgTypes[i].Flags & SignatureType.SignatureTypeFlags.GENERIC) != 0)
                {
                    ArgTypes[i].GenericInstance = _genericParamInstanceMap[ArgTypes[i].TypeName];
                }
            }
        }
Exemple #3
0
 private static bool MatchesTypeParameterCount(
     ImmutableArray <string> typeArguments,
     GenericParameterHandleCollection typeParameters,
     int offset
     )
 {
     return(typeArguments.Length == typeParameters.Count - offset);
 }
 private static ImmutableArray <string> GetTypeParameterNames(
     MetadataReader reader,
     GenericParameterHandleCollection handles
     )
 {
     return(ImmutableArray.CreateRange(
                handles.Select(h => reader.GetString(reader.GetGenericParameter(h).Name))
                ));
 }
Exemple #5
0
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out GenericParameterHandleCollection values)
        {
            values = new GenericParameterHandleCollection(reader, offset);
            uint count;

            offset = reader.DecodeUnsigned(offset, out count);
            for (uint i = 0; i < count; ++i)
            {
                offset = reader.SkipInteger(offset);
            }
            return(offset);
        } // Read
Exemple #6
0
        internal static void CheckTypeParameters(
            this MetadataReader reader,
            GenericParameterHandleCollection genericParameters,
            params string[] expectedNames
            )
        {
            var actualNames = genericParameters
                              .Select(reader.GetGenericParameter)
                              .Select(tp => reader.GetString(tp.Name))
                              .ToArray();

            Assert.True(expectedNames.SequenceEqual(actualNames));
        }
Exemple #7
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(int index, MetadataReader mdReader, uint rid, int entryPointId, CorElementType[] instanceArgs, uint[] tok, FixupCell[] fixups)
        {
            Index = index;
            Token = _mdtMethodDef | rid;
            Rid   = rid;
            EntryPointRuntimeFunctionId = entryPointId;

            _mdReader        = mdReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);

            _methodDef = mdReader.GetMethodDefinition(methodDefHandle);
            Name       = mdReader.GetString(_methodDef.Name);
            BlobReader signatureReader = mdReader.GetBlobReader(_methodDef.Signature);

            TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();

            DeclaringType = MetadataNameFormatter.FormatHandle(mdReader, declaringTypeHandle);

            SignatureHeader signatureHeader = signatureReader.ReadSignatureHeader();

            IsGeneric = signatureHeader.IsGeneric;
            GenericParameterHandleCollection genericParams = _methodDef.GetGenericParameters();

            _genericParamInstanceMap = new Dictionary <string, string>();

            int argCount = signatureReader.ReadCompressedInteger();

            if (IsGeneric)
            {
                argCount = signatureReader.ReadCompressedInteger();
            }

            Fixups = fixups;

            DisassemblingTypeProvider provider = new DisassemblingTypeProvider();

            if (IsGeneric && instanceArgs != null && tok != null)
            {
                InitGenericInstances(genericParams, instanceArgs, tok);
            }

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(new string[0], _genericParamInstanceMap.Values.ToArray());

            Signature = _methodDef.DecodeSignature(provider, genericContext);

            SignatureString = GetSignature();
        }
Exemple #8
0
            public static Handle GetHandleAt(GenericParameterHandleCollection collection, int index)
            {
                int currentIndex = 0;

                foreach (var currentArg in collection)
                {
                    if (currentIndex == index)
                    {
                        return(currentArg);
                    }
                    currentIndex++;
                }

                Debug.Assert(false);
                return(default(Handle));
            }
Exemple #9
0
        public static ITypeParameter[] Create(MetadataModule module, IEntity owner, GenericParameterHandleCollection handles)
        {
            if (handles.Count == 0)
            {
                return(Empty <ITypeParameter> .Array);
            }
            var tps = new ITypeParameter[handles.Count];
            int i   = 0;

            foreach (var handle in handles)
            {
                tps[i] = Create(module, owner, i, handle);
                i++;
            }
            return(tps);
        }
Exemple #10
0
 static bool FindUsesInGenericConstraints(MetadataReader metadata, GenericParameterHandleCollection collection, FindTypeDecoder decoder)
 {
     foreach (var h in collection)
     {
         var gp = metadata.GetGenericParameter(h);
         foreach (var hc in gp.GetConstraints())
         {
             var gc = metadata.GetGenericParameterConstraint(hc);
             if (decoder.GetTypeFromEntity(metadata, gc.Type))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #11
0
        public                                  RoType[] ComputeGenericArgumentsOrParameters()
        {
            GenericParameterHandleCollection gphs = MethodDefinition.GetGenericParameters();
            int count = gphs.Count;

            if (count == 0)
            {
                return(Array.Empty <RoType>());
            }

            RoType[] gps = new RoType[count];
            foreach (GenericParameterHandle gph in gphs)
            {
                RoType gp = gph.ResolveGenericParameter(_module);
                gps[gp.GenericParameterPosition] = gp;
            }
            return(gps);
        }
        private RoType[] ComputeGenericTypeParameters()
        {
            EcmaModule module = GetEcmaModule();
            GenericParameterHandleCollection gps = TypeDefinition.GetGenericParameters();

            if (gps.Count == 0)
            {
                return(Array.Empty <RoType>());
            }

            RoType[] genericParameters = new RoType[gps.Count];
            foreach (GenericParameterHandle h in gps)
            {
                RoType gp = h.ResolveGenericParameter(module);
                genericParameters[gp.GenericParameterPosition] = gp;
            }
            return(genericParameters);
        }
Exemple #13
0
        /// <summary>
        /// Initialize map of generic parameters names to the type in the instance
        /// </summary>
        private void InitGenericInstances(GenericParameterHandleCollection genericParams, CorElementType[] instanceArgs, uint[] tok)
        {
            if (instanceArgs.Length != genericParams.Count || tok.Length != genericParams.Count)
            {
                throw new BadImageFormatException("Generic param indices out of bounds");
            }

            for (int i = 0; i < instanceArgs.Length; i++)
            {
                string key  = _mdReader.GetString(_mdReader.GetGenericParameter(genericParams.ElementAt(i)).Name); // name of the generic param, eg. "T"
                string type = instanceArgs[i].ToString();                                                          // type of the generic param instance
                if (instanceArgs[i] == CorElementType.ELEMENT_TYPE_VALUETYPE)
                {
                    var t = _mdReader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle((int)tok[i]));
                    type = _mdReader.GetString(t.Name); // name of the struct
                }
                _genericParamInstanceMap[key] = type;
            }
        }
Exemple #14
0
        private void InitGenericInstances(GenericParameterHandleCollection genericParams, GenericElementTypes[] instanceArgs, uint[] tok)
        {
            if (instanceArgs.Length != genericParams.Count || tok.Length != genericParams.Count)
            {
                throw new BadImageFormatException("Generic param indices out of bounds");
            }

            for (int i = 0; i < instanceArgs.Length; i++)
            {
                string key  = _mdReader.GetString(_mdReader.GetGenericParameter(genericParams.ElementAt(i)).Name);
                string name = instanceArgs[i].ToString();
                if (instanceArgs[i] == GenericElementTypes.ValueType)
                {
                    var t = _mdReader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle((int)tok[i]));
                    name = _mdReader.GetString(t.Name);
                }
                _genericParamInstanceMap[key] = name;
            }
        }
        public                                          RuntimeTypeInfo[] GetGenericTypeParametersWithSpecifiedOwningMethod(RuntimeNamedMethodInfo <EcmaFormatMethodCommon> owningMethod)
        {
            GenericParameterHandleCollection genericParameters = _method.GetGenericParameters();
            int genericParametersCount = genericParameters.Count;

            if (genericParametersCount == 0)
            {
                return(Array.Empty <RuntimeTypeInfo>());
            }

            RuntimeTypeInfo[] genericTypeParameters = new RuntimeTypeInfo[genericParametersCount];
            int i = 0;

            foreach (GenericParameterHandle genericParameterHandle in genericParameters)
            {
                RuntimeTypeInfo genericParameterType = EcmaFormatRuntimeGenericParameterTypeInfoForMethods.GetRuntimeGenericParameterTypeInfoForMethods(owningMethod, Reader, genericParameterHandle);
                genericTypeParameters[i++] = genericParameterType;
            }
            return(genericTypeParameters);
        }
Exemple #16
0
        private void ImportGenericConstraints(EntityHandle entityHandle, GenericParameterHandleCollection srcParams)
        {
            var srcConstraints = new List <Tuple <GenericParameterHandle, GenericParameterConstraintHandle> >();

            foreach (var srcParamHandle in srcParams)
            {
                var srcParam       = _reader.GetGenericParameter(srcParamHandle);
                var dstParamHandle = _builder.AddGenericParameter(entityHandle, srcParam.Attributes,
                                                                  ImportValue(srcParam.Name), srcParam.Index);
                _genericParameterCache.Add(srcParamHandle, dstParamHandle);
                srcConstraints.AddRange(srcParam.GetConstraints().Select(x => Tuple.Create(dstParamHandle, x)));

                Trace?.Invoke($"Imported generic parameter {_reader.ToString(srcParam)} -> {RowId(dstParamHandle):X}");
            }

            foreach (var(dstParam, srcConstraintHandle) in srcConstraints)
            {
                ImportEntity(srcConstraintHandle, _genericParameterConstraintCache,
                             _reader.GetGenericParameterConstraint,
                             src => _builder.AddGenericParameterConstraint(dstParam, Import(src.Type)),
                             _reader.ToString, IsNil);
            }
        }
        private static ImmutableArray <string> DecodeTypeParameters(
            MetadataReader reader,
            int offset,
            GenericParameterHandleCollection typeParameters
            )
        {
            int arity = typeParameters.Count - offset;

            Debug.Assert(arity >= 0);
            if (arity == 0)
            {
                return(ImmutableArray <string> .Empty);
            }
            var builder = ImmutableArray.CreateBuilder <string>(arity);

            for (int i = 0; i < arity; i++)
            {
                var handle        = typeParameters[offset + i];
                var typeParameter = reader.GetGenericParameter(handle);
                builder.Add(reader.GetString(typeParameter.Name));
            }
            return(builder.ToImmutable());
        }
Exemple #18
0
        public SignatureType(ref BlobReader signatureReader, MetadataReader mdReader, GenericParameterHandleCollection genericParams)
        {
            SignatureTypeCode signatureTypeCode = signatureReader.ReadSignatureTypeCode();

            Flags = 0;
            if (signatureTypeCode == SignatureTypeCode.SZArray)
            {
                Flags            |= SignatureTypeFlags.ARRAY;
                signatureTypeCode = signatureReader.ReadSignatureTypeCode();
            }

            TypeName = signatureTypeCode.ToString();
            if (signatureTypeCode == SignatureTypeCode.TypeHandle || signatureTypeCode == SignatureTypeCode.ByReference)
            {
                if (signatureTypeCode == SignatureTypeCode.ByReference)
                {
                    Flags |= SignatureTypeFlags.REFERENCE;
                }

                EntityHandle handle = signatureReader.ReadTypeHandle();
                if (handle.Kind == HandleKind.TypeDefinition)
                {
                    TypeDefinition typeDef = mdReader.GetTypeDefinition((TypeDefinitionHandle)handle);
                    TypeName = mdReader.GetString(typeDef.Name);
                }
                else if (handle.Kind == HandleKind.TypeReference)
                {
                    TypeReference typeRef = mdReader.GetTypeReference((TypeReferenceHandle)handle);
                    TypeName = mdReader.GetString(typeRef.Name);
                }
            }
            else if (signatureTypeCode == SignatureTypeCode.GenericMethodParameter)
            {
                int index = signatureReader.ReadCompressedInteger();
                GenericParameter generic = mdReader.GetGenericParameter(genericParams[index]);
                TypeName = mdReader.GetString(generic.Name);
                Flags   |= SignatureTypeFlags.GENERIC;
            }
        }
Exemple #19
0
 public GenericContext(GenericParameterHandleCollection parameters, GenericParameterHandleCollection typeParameters, MetadataReader reader)
 {
     Parameters     = parameters;
     TypeParameters = typeParameters;
     Reader         = reader;
 }
Exemple #20
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(byte[] image, MetadataReader mdReader, uint rid, int entryPointId, GenericElementTypes[] instanceArgs, uint[] tok)
        {
            Token = _mdtMethodDef | rid;
            Rid   = rid;
            EntryPointRuntimeFunctionId = entryPointId;

            _mdReader        = mdReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);

            _methodDef = mdReader.GetMethodDefinition(methodDefHandle);
            Name       = mdReader.GetString(_methodDef.Name);
            BlobReader signatureReader = mdReader.GetBlobReader(_methodDef.Signature);

            TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();
            TypeDefinition       declaringTypeDef;

            while (!declaringTypeHandle.IsNil)
            {
                declaringTypeDef    = mdReader.GetTypeDefinition(declaringTypeHandle);
                DeclaringType       = mdReader.GetString(declaringTypeDef.Name) + "." + DeclaringType;
                declaringTypeHandle = declaringTypeDef.GetDeclaringType();
            }

            NamespaceDefinitionHandle namespaceHandle = declaringTypeDef.NamespaceDefinition;

            while (!namespaceHandle.IsNil)
            {
                NamespaceDefinition namespaceDef = mdReader.GetNamespaceDefinition(namespaceHandle);
                DeclaringType   = mdReader.GetString(namespaceDef.Name) + "." + DeclaringType;
                namespaceHandle = namespaceDef.Parent;
            }

            SignatureHeader signatureHeader = signatureReader.ReadSignatureHeader();

            IsGeneric = signatureHeader.IsGeneric;
            GenericParameterHandleCollection genericParams = _methodDef.GetGenericParameters();

            _genericParamInstanceMap = new Dictionary <string, string>();

            int argCount = signatureReader.ReadCompressedInteger();

            if (IsGeneric)
            {
                argCount = signatureReader.ReadCompressedInteger();
            }

            DisassemblingTypeProvider provider = new DisassemblingTypeProvider();

            if (IsGeneric && instanceArgs != null && tok != null)
            {
                InitGenericInstances(genericParams, instanceArgs, tok);
            }

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(new string[0], _genericParamInstanceMap.Values.ToArray());

            Signature = _methodDef.DecodeSignature(provider, genericContext);

            SignatureString = GetSignature();
        }
Exemple #21
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(
            int index,
            R2RReader r2rReader,
            EntityHandle methodHandle,
            int entryPointId,
            string owningType,
            string constrainedType,
            string[] instanceArgs,
            FixupCell[] fixups)
        {
            Index        = index;
            MethodHandle = methodHandle;
            EntryPointRuntimeFunctionId = entryPointId;

            R2RReader        = r2rReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            EntityHandle owningTypeHandle;
            GenericParameterHandleCollection genericParams = default(GenericParameterHandleCollection);

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(typeParameters: Array.Empty <string>(), methodParameters: instanceArgs);
            DisassemblingTypeProvider   typeProvider   = new DisassemblingTypeProvider();

            // get the method signature from the method handle
            switch (MethodHandle.Kind)
            {
            case HandleKind.MethodDefinition:
            {
                MethodDefinition methodDef = R2RReader.MetadataReader.GetMethodDefinition((MethodDefinitionHandle)MethodHandle);
                Name             = R2RReader.MetadataReader.GetString(methodDef.Name);
                Signature        = methodDef.DecodeSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = methodDef.GetDeclaringType();
                genericParams    = methodDef.GetGenericParameters();
            }
            break;

            case HandleKind.MemberReference:
            {
                MemberReference memberRef = R2RReader.MetadataReader.GetMemberReference((MemberReferenceHandle)MethodHandle);
                Name             = R2RReader.MetadataReader.GetString(memberRef.Name);
                Signature        = memberRef.DecodeMethodSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = memberRef.Parent;
            }
            break;

            default:
                throw new NotImplementedException();
            }

            if (owningType != null)
            {
                DeclaringType = owningType;
            }
            else
            {
                DeclaringType = MetadataNameFormatter.FormatHandle(R2RReader.MetadataReader, owningTypeHandle);
            }

            Fixups = fixups;

            StringBuilder sb = new StringBuilder();

            sb.Append(Signature.ReturnType);
            sb.Append(" ");
            sb.Append(DeclaringType);
            sb.Append(".");
            sb.Append(Name);

            if (Signature.GenericParameterCount != 0)
            {
                sb.Append("<");
                for (int i = 0; i < Signature.GenericParameterCount; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    if (instanceArgs != null && instanceArgs.Length > i)
                    {
                        sb.Append(instanceArgs[i]);
                    }
                    else
                    {
                        sb.Append("!");
                        sb.Append(i);
                    }
                }
                sb.Append(">");
            }

            sb.Append("(");
            for (int i = 0; i < Signature.ParameterTypes.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                sb.AppendFormat($"{Signature.ParameterTypes[i]}");
            }
            sb.Append(")");

            SignatureString = sb.ToString();
        }
Exemple #22
0
        public static ITypeParameter[] Create(MetadataModule module, ITypeDefinition copyFromOuter, IEntity owner, GenericParameterHandleCollection handles)
        {
            if (handles.Count == 0)
            {
                return(Empty <ITypeParameter> .Array);
            }
            var outerTps = copyFromOuter.TypeParameters;
            var tps      = new ITypeParameter[handles.Count];
            int i        = 0;

            foreach (var handle in handles)
            {
                if (i < outerTps.Count)
                {
                    tps[i] = outerTps[i];
                }
                else
                {
                    tps[i] = Create(module, owner, i, handle);
                }
                i++;
            }
            return(tps);
        }
Exemple #23
0
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out GenericParameterHandleCollection values)
        {
            values = new GenericParameterHandleCollection(reader, offset);
            uint count;
            offset = reader.DecodeUnsigned(offset, out count);
            for (uint i = 0; i < count; ++i)
            {
                offset = reader.SkipInteger(offset);
            }
            return offset;
        } // Read
Exemple #24
0
            private static void FillArray(IMetadataGenericTypeParameter[] array, GenericContext genericContext, MetadataReader reader, TypeReferenceTypeProvider typeProvider, GenericParameterHandleCollection genericParameters)
            {
                foreach (var handle in genericParameters)
                {
                    var genericParameter = reader.GetGenericParameter(handle);

                    // It's okay to pass in genericContext as we fill the array.
                    // It's coupling to an implementation detail in the same class.
                    array[genericParameter.Index] = new ReaderGenericTypeParameter(reader, typeProvider, genericParameter, genericContext);
                }
            }
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public ReadyToRunMethod(
            ReadyToRunReader readyToRunReader,
            IAssemblyMetadata componentReader,
            EntityHandle methodHandle,
            int entryPointId,
            string owningType,
            string constrainedType,
            string[] instanceArgs,
            int?fixupOffset)
        {
            _readyToRunReader           = readyToRunReader;
            _fixupOffset                = fixupOffset;
            MethodHandle                = methodHandle;
            EntryPointRuntimeFunctionId = entryPointId;

            ComponentReader = componentReader;

            EntityHandle owningTypeHandle;
            GenericParameterHandleCollection genericParams = default(GenericParameterHandleCollection);

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(typeParameters: Array.Empty <string>(), methodParameters: instanceArgs);
            DisassemblingTypeProvider   typeProvider   = new DisassemblingTypeProvider();

            // get the method signature from the method handle
            switch (MethodHandle.Kind)
            {
            case HandleKind.MethodDefinition:
            {
                MethodDefinition methodDef = ComponentReader.MetadataReader.GetMethodDefinition((MethodDefinitionHandle)MethodHandle);
                if (methodDef.RelativeVirtualAddress != 0)
                {
                    MethodBodyBlock mbb = ComponentReader.ImageReader.GetMethodBody(methodDef.RelativeVirtualAddress);
                    if (!mbb.LocalSignature.IsNil)
                    {
                        StandaloneSignature ss = ComponentReader.MetadataReader.GetStandaloneSignature(mbb.LocalSignature);
                        LocalSignature = ss.DecodeLocalSignature(typeProvider, genericContext);
                    }
                }
                Name             = ComponentReader.MetadataReader.GetString(methodDef.Name);
                Signature        = methodDef.DecodeSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = methodDef.GetDeclaringType();
                genericParams    = methodDef.GetGenericParameters();
            }
            break;

            case HandleKind.MemberReference:
            {
                MemberReference memberRef = ComponentReader.MetadataReader.GetMemberReference((MemberReferenceHandle)MethodHandle);
                Name             = ComponentReader.MetadataReader.GetString(memberRef.Name);
                Signature        = memberRef.DecodeMethodSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = memberRef.Parent;
            }
            break;

            default:
                throw new NotImplementedException();
            }

            if (owningType != null)
            {
                DeclaringType = owningType;
            }
            else
            {
                DeclaringType = MetadataNameFormatter.FormatHandle(ComponentReader.MetadataReader, owningTypeHandle);
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(Signature.ReturnType);
            sb.Append(" ");
            sb.Append(DeclaringType);
            sb.Append(".");
            sb.Append(Name);

            if (Signature.GenericParameterCount != 0)
            {
                sb.Append("<");
                for (int i = 0; i < Signature.GenericParameterCount; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    if (instanceArgs != null && instanceArgs.Length > i)
                    {
                        sb.Append(instanceArgs[i]);
                    }
                    else
                    {
                        sb.Append("!");
                        sb.Append(i);
                    }
                }
                sb.Append(">");
            }

            sb.Append("(");
            for (int i = 0; i < Signature.ParameterTypes.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                sb.AppendFormat($"{Signature.ParameterTypes[i]}");
            }
            sb.Append(")");

            SignatureString = sb.ToString();
        }