public static CustomAttributeArgument FromReader(MetadataHeader header, TypeSignature typeSignature, IBinaryStreamReader reader) { var signature = new CustomAttributeArgument() { StartOffset = reader.Position, ArgumentType = typeSignature }; if (typeSignature.ElementType != ElementType.SzArray) { signature.Elements.Add(ElementSignature.FromReader(header, typeSignature, reader)); } else { var arrayType = ((SzArrayTypeSignature)typeSignature).BaseType; var elementCount = reader.CanRead(sizeof (uint)) ? reader.ReadUInt32() : uint.MaxValue; if (elementCount != uint.MaxValue) { for (uint i = 0; i < elementCount; i++) { signature.Elements.Add(ElementSignature.FromReader(header, arrayType, reader)); } } } return signature; }
public CustomAttributeNamedArgument(CustomAttributeArgumentMemberType argumentMemberType, TypeSignature argumentType, string memberName, CustomAttributeArgument argument) { ArgumentMemberType = argumentMemberType; ArgumentType = argumentType; MemberName = memberName; Argument = argument; }
public static ElementSignature FromReader(MetadataHeader header, TypeSignature typeSignature, IBinaryStreamReader reader) { long position = reader.Position; return new ElementSignature(ReadValue(header, typeSignature, reader)) { StartOffset = position }; }
public CustomAttributeArgument(TypeSignature argumentType, params ElementSignature[] values) : this() { if (argumentType == null) throw new ArgumentNullException("argumentType"); if (values == null) throw new ArgumentNullException("values"); ArgumentType = argumentType; foreach (var value in values) Elements.Add(value); }
private static string GetFullName(TypeSignature signature) { var specification = signature as TypeSpecificationSignature; if (specification != null) return GetFullName(specification); var typeDefOrRef = signature as TypeDefOrRefSignature; if (typeDefOrRef != null) return typeDefOrRef.Type.FullName; var corlibType = signature as MsCorLibTypeSignature; if (corlibType != null) return corlibType.FullName; throw new NotSupportedException("Invalid or unsupported type signature: " + signature.FullName + "."); }
public PointerTypeSignature(TypeSignature baseType) : base(baseType) { }
public new static RequiredModifierSignature FromReader(MetadataHeader header, IBinaryStreamReader reader) { return(new RequiredModifierSignature(ReadTypeDefOrRef(header, reader), TypeSignature.FromReader(header, reader))); }
public OptionalModifierSignature(ITypeDefOrRef modifierType, TypeSignature baseType) : base(baseType) { ModifierType = modifierType; }
public ParameterSignature(TypeSignature parameterType) { ParameterType = parameterType; }
public static ParameterSignature FromReader(MetadataImage image, IBinaryStreamReader reader) { return(new ParameterSignature(TypeSignature.FromReader(image, reader))); }
public FieldSignature(TypeSignature fieldType) { Attributes = CallingConventionAttributes.Field; FieldType = fieldType; }
private static void TestFieldRva(WindowsAssembly assembly, TypeSignature fieldType, byte[] fieldRvaData, bool saveToDisk) { // set up temp assembly. var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); var fieldTable = tableStream.GetTable<FieldDefinition>(); var fieldRvaTable = tableStream.GetTable<FieldRva>(); // create temp field. var field = new FieldDefinition(FieldName, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.HasFieldRva, new FieldSignature(fieldType)); fieldTable.Add(field); // create field rva. var fieldRva = new FieldRva(field, fieldRvaData); field.FieldRva = fieldRva; fieldRvaTable.Add(fieldRva); assembly = Utilities.RebuildNetAssembly(assembly, saveToDisk); tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); fieldTable = tableStream.GetTable<FieldDefinition>(); field = fieldTable.FirstOrDefault(x => x.Name == FieldName); Assert.IsNotNull(field); Assert.IsNotNull(field.FieldRva); Utilities.ValidateByteArrays(fieldRvaData, field.FieldRva.Data); }
public MethodSignature(IEnumerable<TypeSignature> parameters, TypeSignature returnType) : this(parameters.Select(x => new ParameterSignature(x)), returnType) { }
public PropertySignature(TypeSignature propertyType, IEnumerable <ParameterSignature> parameters) { PropertyType = propertyType; Parameters = new List <ParameterSignature>(parameters); }
/// <summary> /// Reads a single array type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the array was defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read array.</returns> public static ArrayTypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var signature = new ArrayTypeSignature(TypeSignature.FromReader(image, reader, false, protection)); // Rank if (!reader.TryReadCompressedUInt32(out uint rank)) { return(signature); } // Sizes. if (!reader.TryReadCompressedUInt32(out uint numSizes)) { return(signature); } var sizes = new List <uint>(); for (int i = 0; i < numSizes; i++) { if (!reader.TryReadCompressedUInt32(out uint size)) { return(signature); } sizes.Add(size); } // Lower bounds. if (!reader.TryReadCompressedUInt32(out uint numLoBounds)) { return(signature); } var loBounds = new List <uint>(); for (int i = 0; i < numLoBounds; i++) { if (!reader.TryReadCompressedUInt32(out uint bound)) { return(signature); } loBounds.Add(bound); } // Create dimensions. for (int i = 0; i < rank; i++) { var dimension = new ArrayDimension(); if (i < numSizes) { dimension.Size = (int)sizes[i]; } if (i < numLoBounds) { dimension.LowerBound = (int)loBounds[i]; } signature.Dimensions.Add(dimension); } return(signature); }
public PropertySignature(TypeSignature propertyType) : this(propertyType, Enumerable.Empty <ParameterSignature>()) { }
public CustomAttributeArgument(TypeSignature argumentType, ElementSignature value) : this(argumentType, new[] { value }) { }
/// <summary> /// Reads a single boxed type signature at the current position of the provided stream reader. /// </summary> /// <param name="image">The image the signature is defined in.</param> /// <param name="reader">The reader to use.</param> /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param> /// <returns>The read signature.</returns> public static BoxedTypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { return(new BoxedTypeSignature(TypeSignature.FromReader(image, reader, false, protection))); }
private static void VerifyMatching(TypeSignature original, TypeSignature expected, params TypeSignature[] fails) { Assert.IsTrue(_comparer.MatchTypes(original, expected), "The original signature did not match the expected."); Assert.IsTrue(_comparer.MatchTypes(expected, original), "The expected signature did not match the original."); foreach (var fail in fails) { Assert.IsFalse(_comparer.MatchTypes(fail, original), original + " matched " + fail.FullName); Assert.IsFalse(_comparer.MatchTypes(original, fail), fail.FullName + " matched " + original); } var dummyType = CreateTypeRef(original.Namespace, original.Name); Assert.IsFalse(_comparer.MatchTypes(original, dummyType), original.FullName + " matched the dummy type."); Assert.IsFalse(_comparer.MatchTypes(dummyType, original), "The dummy type for " + original.FullName + " matched the original."); }
public PinnedTypeSignature(TypeSignature baseType) : base(baseType) { }
public ByReferenceTypeSignature(TypeSignature baseType) : base(baseType) { }
public ArrayTypeSignature(TypeSignature baseType) : base(baseType) { Dimensions = new List<ArrayDimension>(); }
public VariableSignature(TypeSignature variableType) { VariableType = variableType; }
public MethodSignature(IEnumerable <TypeSignature> parameters, TypeSignature returnType) : this(parameters.Select(x => new ParameterSignature(x)), returnType) { }
public SzArrayTypeSignature(TypeSignature baseType) : base(baseType) { }
private static object ReadValue(MetadataImage image, TypeSignature typeSignature, IBinaryStreamReader reader) { switch (typeSignature.ElementType) { case ElementType.Boolean: return(reader.ReadByte() == 1); case ElementType.Char: return((char)reader.ReadUInt16()); case ElementType.R4: return(reader.ReadSingle()); case ElementType.R8: return(reader.ReadDouble()); case ElementType.I1: return(reader.ReadSByte()); case ElementType.I2: return(reader.ReadInt16()); case ElementType.I4: return(reader.ReadInt32()); case ElementType.I8: return(reader.ReadInt64()); case ElementType.U1: return(reader.ReadByte()); case ElementType.U2: return(reader.ReadUInt16()); case ElementType.U4: return(reader.ReadUInt32()); case ElementType.U8: return(reader.ReadUInt64()); case ElementType.String: return(reader.ReadSerString()); case ElementType.Object: return(ReadValue(image, TypeSignature.ReadFieldOrPropType(image, reader), reader)); case ElementType.Class: case ElementType.Enum: case ElementType.ValueType: var enumTypeDef = image.MetadataResolver.ResolveType(typeSignature); if (enumTypeDef == null) { throw new MemberResolutionException(typeSignature); } if (enumTypeDef.IsEnum) { return(ReadValue(image, enumTypeDef.GetEnumUnderlyingType(), reader)); } break; } if (typeSignature.IsTypeOf("System", "Type")) { return(TypeSignature.FromAssemblyQualifiedName(image, reader.ReadSerString())); } throw new NotSupportedException("Unsupported element type " + typeSignature.ElementType); }
public SentinelTypeSignature(TypeSignature baseType) : base(baseType) { }
public MethodSignature(TypeSignature returnType) : this(Enumerable.Empty <ParameterSignature>(), returnType) { ReturnType = returnType; }
public static ElementSignature FromReader(MetadataImage image, TypeSignature typeSignature, IBinaryStreamReader reader) { return(new ElementSignature(ReadValue(image, typeSignature, reader))); }
public MethodSignature(IEnumerable <ParameterSignature> parameters, TypeSignature returnType) { Parameters = new List <ParameterSignature>(parameters); ReturnType = returnType; }
public new static SzArrayTypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader) { return(new SzArrayTypeSignature(TypeSignature.FromReader(image, reader))); }
public BoxedTypeSignature(TypeSignature baseType) : base(baseType) { }
public new static PointerTypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader) { long position = reader.Position; return(new PointerTypeSignature(TypeSignature.FromReader(image, reader))); }
public new static ArrayTypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader) { long position = reader.Position; var signature = new ArrayTypeSignature(TypeSignature.FromReader(header, reader)) { StartOffset = position }; uint rank; if (!reader.TryReadCompressedUInt32(out rank)) { return(signature); } uint numSizes; if (!reader.TryReadCompressedUInt32(out numSizes)) { return(signature); } var sizes = new uint[numSizes]; for (int i = 0; i < numSizes; i++) { if (!reader.TryReadCompressedUInt32(out sizes[i])) { return(signature); } } uint numLoBounds; if (!reader.TryReadCompressedUInt32(out numLoBounds)) { return(signature); } var loBounds = new uint[numLoBounds]; for (int i = 0; i < numLoBounds; i++) { if (!reader.TryReadCompressedUInt32(out loBounds[i])) { return(signature); } } for (int i = 0; i < rank; i++) { var dimension = new ArrayDimension(); if (i < numSizes) { dimension.Size = (int)sizes[i]; } if (i < numLoBounds) { dimension.LowerBound = (int)loBounds[i]; } signature.Dimensions.Add(dimension); } return(signature); }
public RequiredModifierSignature(ITypeDefOrRef modifierType, TypeSignature baseType) : base(baseType) { ModifierType = modifierType; }
public ArrayTypeSignature(TypeSignature baseType) : base(baseType) { Dimensions = new List <ArrayDimension>(); }
public MethodSignature(TypeSignature returnType) : this(Enumerable.Empty<ParameterSignature>(), returnType) { ReturnType = returnType; }
public MethodSignature(IEnumerable<ParameterSignature> parameters, TypeSignature returnType) { Parameters = new List<ParameterSignature>(parameters); ReturnType = returnType; }
public new static PointerTypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader) { return(new PointerTypeSignature(TypeSignature.FromReader(header, reader))); }
private static object ReadValue(MetadataHeader header, TypeSignature typeSignature, IBinaryStreamReader reader) { switch (typeSignature.ElementType) { case ElementType.Boolean: return reader.ReadByte() == 1; case ElementType.Char: return (char)reader.ReadUInt16(); case ElementType.R4: return reader.ReadSingle(); case ElementType.R8: return reader.ReadDouble(); case ElementType.I1: return reader.ReadSByte(); case ElementType.I2: return reader.ReadInt16(); case ElementType.I4: return reader.ReadInt32(); case ElementType.I8: return reader.ReadInt64(); case ElementType.U1: return reader.ReadByte(); case ElementType.U2: return reader.ReadUInt16(); case ElementType.U4: return reader.ReadUInt32(); case ElementType.U8: return reader.ReadUInt64(); case ElementType.String: return reader.ReadSerString(); case ElementType.Object: return ReadValue(header, TypeSignature.ReadFieldOrPropType(header, reader), reader); case ElementType.Class: case ElementType.Enum: case ElementType.ValueType: var enumTypeDef = header.MetadataResolver.ResolveType(typeSignature); if (enumTypeDef == null) throw new MemberResolutionException(typeSignature); if (enumTypeDef.IsEnum) return ReadValue(header, enumTypeDef.GetEnumUnderlyingType(), reader); break; } if (typeSignature.IsTypeOf("System", "Type")) return TypeSignature.FromAssemblyQualifiedName(header, reader.ReadSerString()); throw new NotSupportedException("Unsupported element type " + typeSignature.ElementType); }
protected TypeSpecificationSignature(TypeSignature baseType) { BaseType = baseType; }
public TypeSignature ImportTypeSignature(TypeSignature signature) { if (signature is MsCorLibTypeSignature) return signature; var typeDefOrRef = signature as TypeDefOrRefSignature; if (typeDefOrRef != null) return ImportTypeDefOrRefSignature(typeDefOrRef); var corlibType = signature as MsCorLibTypeSignature; if (corlibType != null) return ImportCorlibTypeSignature(corlibType); var arrayType = signature as ArrayTypeSignature; if (arrayType != null) return ImportArrayTypeSignature(arrayType); var boxedType = signature as BoxedTypeSignature; if (boxedType != null) return ImportBoxedTypeSignature(boxedType); var byRefType = signature as ByReferenceTypeSignature; if (byRefType != null) return ImportByRefTypeSignature(byRefType); var functionPtrType = signature as FunctionPointerTypeSignature; if (functionPtrType != null) return ImportFunctionPointerTypeSignature(functionPtrType); var genericType = signature as GenericInstanceTypeSignature; if (genericType != null) return ImportGenericInstanceTypeSignature(genericType); var modOptType = signature as OptionalModifierSignature; if (modOptType != null) return ImportOptionalModifierSignature(modOptType); var pinnedType = signature as PinnedTypeSignature; if (pinnedType != null) return ImportPinnedTypeSignature(pinnedType); var pointerType = signature as PointerTypeSignature; if (pointerType != null) return ImportPointerTypeSignature(pointerType); var modReqType = signature as RequiredModifierSignature; if (modReqType != null) return ImportRequiredModifierSignature(modReqType); var sentinelType = signature as SentinelTypeSignature; if (sentinelType != null) return ImportSentinelTypeSignature(sentinelType); var szArrayType = signature as SzArrayTypeSignature; if (szArrayType != null) return ImportSzArrayTypeSignature(szArrayType); throw new NotSupportedException("Invalid or unsupported type signature."); }
/// <summary> /// Determines whether two types are considered equal according to their signature. /// </summary> /// <param name="signature1">The first type to compare.</param> /// <param name="descriptor">The second type to compare.</param> /// <returns><c>True</c> if the types are considered equal, <c>False</c> otherwise.</returns> public bool MatchTypes(TypeSignature signature1, ITypeDescriptor descriptor) { if (signature1 == null && descriptor == null) return true; if (signature1 == null || descriptor == null) return false; var signature2 = descriptor as TypeSignature; if (signature2 != null) return MatchTypes(signature1, signature2); var typeDefOrRefSig = signature1 as TypeDefOrRefSignature; if (typeDefOrRefSig != null) return MatchTypes(typeDefOrRefSig, descriptor); var corlibType = signature1 as MsCorLibTypeSignature; if (corlibType != null) return MatchTypes(corlibType, descriptor); return false; }
private static TypeSignature ReadTypeSignature(TypeSignature elementType, string name, ref int position) { if (position < name.Length) { switch (name[position]) { case '*': return new PointerTypeSignature(elementType); case '&': return new ByReferenceTypeSignature(elementType); case '[': position++; if (name[position] == ']') { position++; return new SzArrayTypeSignature(elementType); } // TODO: support generic types + generic instances. break; } } return elementType; }
/// <summary> /// Determines whether two types are considered equal according to their signature. /// </summary> /// <param name="signature1">The first type to compare.</param> /// <param name="signature2">The second type to compare.</param> /// <returns><c>True</c> if the types are considered equal, <c>False</c> otherwise.</returns> public bool MatchTypes(TypeSignature signature1, TypeSignature signature2) { if (signature1 == null && signature2 == null) return true; if (signature1 == null || signature2 == null) return false; var typeDefOrRef = signature1 as TypeDefOrRefSignature; if (typeDefOrRef != null) return MatchTypes(typeDefOrRef, (ITypeDescriptor) signature2); var corlibType = signature1 as MsCorLibTypeSignature; if (corlibType != null) return MatchTypes(corlibType, (ITypeDescriptor) signature2); var arrayType = signature1 as ArrayTypeSignature; if (arrayType != null) return MatchTypes(arrayType, signature2 as ArrayTypeSignature); var boxedType = signature1 as BoxedTypeSignature; if (boxedType != null) return MatchTypes(boxedType, signature2 as BoxedTypeSignature); var byRefType = signature1 as ByReferenceTypeSignature; if (byRefType != null) return MatchTypes(byRefType, signature2 as ByReferenceTypeSignature); var functionPtrType = signature1 as FunctionPointerTypeSignature; if (functionPtrType != null) return MatchTypes(functionPtrType, signature2 as FunctionPointerTypeSignature); var genericType = signature1 as GenericInstanceTypeSignature; if (genericType != null) return MatchTypes(genericType, signature2 as GenericInstanceTypeSignature); var genericParam = signature1 as GenericParameterSignature; if (genericParam != null) return MatchTypes(genericParam, signature2 as GenericParameterSignature); var modOptType = signature1 as OptionalModifierSignature; if (modOptType != null) return MatchTypes(modOptType, signature2 as OptionalModifierSignature); var pinnedType = signature1 as PinnedTypeSignature; if (pinnedType != null) return MatchTypes(pinnedType, signature2 as PinnedTypeSignature); var pointerType = signature1 as PointerTypeSignature; if (pointerType != null) return MatchTypes(pointerType, signature2 as PointerTypeSignature); var modReqType = signature1 as RequiredModifierSignature; if (modReqType != null) return MatchTypes(modReqType, signature2 as RequiredModifierSignature); var sentinelType = signature1 as SentinelTypeSignature; if (sentinelType != null) return MatchTypes(sentinelType, signature2 as SentinelTypeSignature); var szArrayType = signature1 as SzArrayTypeSignature; if (szArrayType != null) return MatchTypes(szArrayType, signature2 as SzArrayTypeSignature); return false; }
public static string GetAssemblyQualifiedName(TypeSignature signature) { string typeName = GetFullName(signature); var assembly = signature.ResolutionScope.GetAssembly(); return typeName + ", " + assembly.FullName; }