private static string GetMemberRef(MetadataReader metadataReader, int token, CilTypeProvider provider, string genericParameterSignature = "") { var refHandle = MetadataTokens.MemberReferenceHandle(token); var reference = metadataReader.GetMemberReference(refHandle); var parentToken = MetadataTokens.GetToken(reference.Parent); string type; if (IsTypeSpecification(parentToken)) { var typeSpecificationHandle = MetadataTokens.TypeSpecificationHandle(parentToken); type = SignatureDecoder.DecodeType(typeSpecificationHandle, provider, null).ToString(); } else { var parentHandle = MetadataTokens.TypeReferenceHandle(parentToken); type = SignatureDecoder.DecodeType(parentHandle, provider, null).ToString(false); } string signatureValue; string parameters = string.Empty; if (reference.GetKind() == MemberReferenceKind.Method) { MethodSignature <CilType> signature = SignatureDecoder.DecodeMethodSignature(reference.Signature, provider); signatureValue = GetMethodReturnType(signature); parameters = provider.GetParameterList(signature); return(String.Format("{0} {1}::{2}{3}{4}", signatureValue, type, GetString(metadataReader, reference.Name), genericParameterSignature, parameters)); } signatureValue = SignatureDecoder.DecodeFieldSignature(reference.Signature, provider).ToString(); return(String.Format("{0} {1}::{2}{3}", signatureValue, type, GetString(metadataReader, reference.Name), parameters)); }
public TType DecodeFieldSignature <TType, TGenericContext>(ISignatureTypeProvider <TType, TGenericContext> provider, TGenericContext genericContext) { var decoder = new SignatureDecoder <TType, TGenericContext>(provider, _reader, genericContext); var blobReader = _reader.GetBlobReader(Signature); return(decoder.DecodeFieldSignature(ref blobReader)); }
TType DecodeSignature <TType>(ISignatureTypeProvider <TType> provider, SignatureDecoderOptions options = SignatureDecoderOptions.None) { var decoder = new SignatureDecoder <TType>(provider, _reader, options); var blob = _reader.GetBlobReader(Signature); return(decoder.DecodeFieldSignature(ref blob)); }
public TType DecodeSignature <TType>(ISignatureTypeProvider <TType> provider) { var decoder = new SignatureDecoder <TType>(provider, _reader); var blob = _reader.GetBlobReader(Signature); return(decoder.DecodeFieldSignature(ref blob)); }
private static string GetFieldInformation(MetadataReader metadataReader, int intOperand, CilTypeProvider provider) { if (IsMemberReference(intOperand)) { return(GetMemberRef(metadataReader, intOperand, provider)); } var handle = MetadataTokens.FieldDefinitionHandle(intOperand); var definition = metadataReader.GetFieldDefinition(handle); var typeHandle = definition.GetDeclaringType(); var typeSignature = SignatureDecoder.DecodeType(typeHandle, provider, null); var signature = SignatureDecoder.DecodeFieldSignature(definition.Signature, provider); return(String.Format("{0} {1}::{2}", signature.ToString(), typeSignature.ToString(false), GetString(metadataReader, definition.Name))); }
public void WrongSignatureType() { using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(typeof(VarArgsToDecode).GetTypeInfo().Assembly))) using (var peReader = new PEReader(stream)) { MetadataReader reader = peReader.GetMetadataReader(); var provider = new DisassemblingTypeProvider(); var decoder = new SignatureDecoder <string, DisassemblingGenericContext>(provider, reader, genericContext: null); BlobReader fieldSignature = reader.GetBlobReader(reader.GetFieldDefinition(MetadataTokens.FieldDefinitionHandle(1)).Signature); BlobReader methodSignature = reader.GetBlobReader(reader.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(1)).Signature); BlobReader propertySignature = reader.GetBlobReader(reader.GetPropertyDefinition(MetadataTokens.PropertyDefinitionHandle(1)).Signature); Assert.Throws <BadImageFormatException>(() => decoder.DecodeMethodSignature(ref fieldSignature)); Assert.Throws <BadImageFormatException>(() => decoder.DecodeFieldSignature(ref methodSignature)); Assert.Throws <BadImageFormatException>(() => decoder.DecodeLocalSignature(ref propertySignature)); } }
private static string DumpRec(this MetadataReader reader, EntityHandle handle) { switch (handle.Kind) { case HandleKind.AssemblyReference: return(reader.GetString(reader.GetAssemblyReference((AssemblyReferenceHandle)handle).Name)); case HandleKind.TypeDefinition: { TypeDefinition type = reader.GetTypeDefinition((TypeDefinitionHandle)handle); return(getQualifiedName(type.Namespace, type.Name)); } case HandleKind.MethodDefinition: { MethodDefinition method = reader.GetMethodDefinition((MethodDefinitionHandle)handle); var blob = reader.GetBlobReader(method.Signature); var decoder = new SignatureDecoder <string, object>(ConstantSignatureVisualizer.Instance, reader, genericContext: null); var signature = decoder.DecodeMethodSignature(ref blob); var parameters = signature.ParameterTypes.Join(", "); return($"{signature.ReturnType} {DumpRec(reader, method.GetDeclaringType())}.{reader.GetString(method.Name)}({parameters})"); } case HandleKind.MemberReference: { MemberReference member = reader.GetMemberReference((MemberReferenceHandle)handle); var blob = reader.GetBlobReader(member.Signature); var decoder = new SignatureDecoder <string, object>(ConstantSignatureVisualizer.Instance, reader, genericContext: null); var signature = decoder.DecodeMethodSignature(ref blob); var parameters = signature.ParameterTypes.Join(", "); return($"{signature.ReturnType} {DumpRec(reader, member.Parent)}.{reader.GetString(member.Name)}({parameters})"); } case HandleKind.TypeReference: { TypeReference type = reader.GetTypeReference((TypeReferenceHandle)handle); return(getQualifiedName(type.Namespace, type.Name)); } case HandleKind.FieldDefinition: { FieldDefinition field = reader.GetFieldDefinition((FieldDefinitionHandle)handle); var name = reader.GetString(field.Name); var blob = reader.GetBlobReader(field.Signature); var decoder = new SignatureDecoder <string, object>(ConstantSignatureVisualizer.Instance, reader, genericContext: null); var type = decoder.DecodeFieldSignature(ref blob); return($"{type} {name}"); } default: return(null); } string getQualifiedName(StringHandle leftHandle, StringHandle rightHandle) { string name = reader.GetString(rightHandle); if (!leftHandle.IsNil) { name = reader.GetString(leftHandle) + "." + name; } return(name); } }