示例#1
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();
        }
示例#2
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();
        }
示例#3
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();
        }