public CustomAttributeOptions(CustomAttribute ca) { this.RawData = ca.RawData; this.Constructor = ca.Constructor; this.ConstructorArguments.AddRange(ca.ConstructorArguments.Select(a => a.Clone())); this.NamedArguments.AddRange(ca.NamedArguments.Select(a => a.Clone())); }
CustomAttribute Read(ICustomAttributeType ctor) { var methodSig = ctor == null ? null : ctor.MethodSig; if (methodSig == null) { throw new CABlobParserException("ctor is null or not a method"); } var mrCtor = ctor as MemberRef; if (mrCtor != null) { var owner = mrCtor.Class as TypeSpec; if (owner != null) { var gis = owner.TypeSig as GenericInstSig; if (gis != null) { genericArguments = new GenericArguments(); genericArguments.PushTypeArgs(gis.GenericArguments); } } } bool isEmpty = methodSig.Params.Count == 0 && reader.Position == reader.Length; if (!isEmpty && reader.ReadUInt16() != 1) { throw new CABlobParserException("Invalid CA blob prolog"); } var ctorArgs = new List <CAArgument>(methodSig.Params.Count); foreach (var arg in methodSig.Params.GetSafeEnumerable()) { ctorArgs.Add(ReadFixedArg(FixTypeSig(arg))); } // Some tools don't write the next ushort if there are no named arguments. int numNamedArgs = reader.Position == reader.Length ? 0 : reader.ReadUInt16(); var namedArgs = ReadNamedArguments(numNamedArgs); // verifyReadAllBytes will be set when we guess the underlying type of an enum. // To make sure we guessed right, verify that we read all bytes. if (verifyReadAllBytes && reader.Position != reader.Length) { throw new CABlobParserException("Not all CA blob bytes were read"); } return(new CustomAttribute(ctor, ctorArgs, namedArgs, CloneBlobReader(reader))); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance or <c>null</c> if one of the /// args is <c>null</c> or if we failed to parse the CA blob</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor, GenericParamContext gpContext) { if (stream == null || ctor == null) { return(null); } try { using (var reader = new CustomAttributeReader(module, stream, gpContext)) return(reader.Read(ctor)); } catch (CABlobParserException) { return(null); } catch (IOException) { return(null); } }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <returns>A new <see cref="CustomAttribute"/> instance or <c>null</c> if one of the /// args is <c>null</c> or if we failed to parse the CA blob</returns> public static CustomAttribute Read(ModuleDef module, IImageStream stream, ICustomAttributeType ctor) { if (stream == null || ctor == null) { return(null); } try { using (var reader = new CustomAttributeReader(module, stream, ctor)) return(reader.Read()); } catch (CABlobParserException) { return(null); } catch (IOException) { return(null); } }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset) { if (ctor == null) { return(CreateEmpty(ctor)); } using (var reader = new CustomAttributeReader(readerModule, ctor, offset)) { try { return(reader.Read()); } catch (CABlobParserException) { return(new CustomAttribute(ctor, reader.GetRawBlob())); } catch (IOException) { return(new CustomAttribute(ctor, reader.GetRawBlob())); } } }
static MethodSig GetMethodSig(ICustomAttributeType ctor) { var mrCtor = ctor as MemberRef; if (mrCtor != null) { return(mrCtor.MethodSig); } var mdCtor = ctor as MethodDef; if (mdCtor != null) { return(mdCtor.MethodSig); } return(null); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset, GenericParamContext gpContext) { using (var reader = new CustomAttributeReader(readerModule, offset, gpContext)) { try { if (ctor == null) { return(reader.CreateRaw(ctor)); } return(reader.Read(ctor)); } catch (CABlobParserException) { return(reader.CreateRaw(ctor)); } catch (IOException) { return(reader.CreateRaw(ctor)); } } }
private uint AddCustomAttributeType(ICustomAttributeType constructor) { if (constructor is null || !AssertIsImported(constructor)) { return(0); } var token = constructor.MetadataToken.Table switch { TableIndex.Method => GetMethodDefinitionToken(constructor as MethodDefinition), TableIndex.MemberRef => GetMemberReferenceToken(constructor as MemberReference), _ => throw new ArgumentOutOfRangeException(nameof(constructor)) }; return(Metadata.TablesStream .GetIndexEncoder(CodedIndex.CustomAttributeType) .EncodeToken(token)); }
static CustomAttribute CreateEmpty(ICustomAttributeType ctor) { return new CustomAttribute(ctor, new byte[0]); }
/// <summary> /// Creates a new custom attribute. /// </summary> /// <param name="constructor">The constructor of the attribute to call.</param> /// <param name="signature">The signature containing the arguments to the constructor.</param> public CustomAttribute(ICustomAttributeType constructor, CustomAttributeSignature signature) : this(new MetadataToken(TableIndex.CustomAttribute, 0)) { Constructor = constructor; Signature = signature; }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor) { return(Read(module, stream, ctor, new GenericParamContext())); }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, ThreadSafe.IEnumerable <CAArgument> arguments) : this(ctor, arguments, null) { }
/// <summary> /// Reads a single custom attribute signature from the input stream. /// </summary> /// <param name="parentModule">The module that contains the attribute signature.</param> /// <param name="ctor">The constructor that was called.</param> /// <param name="reader">The input stream.</param> /// <returns>The signature.</returns> /// <exception cref="FormatException">Occurs when the input stream does not point to a valid signature.</exception> public static CustomAttributeSignature FromReader(ModuleDefinition parentModule, ICustomAttributeType ctor, IBinaryStreamReader reader) { ushort prologue = reader.ReadUInt16(); if (prologue != CustomAttributeSignaturePrologue) { throw new FormatException("Input stream does not point to a valid custom attribute signature."); } var result = new CustomAttributeSignature(); // Read fixed arguments. var parameterTypes = ctor.Signature.ParameterTypes; for (int i = 0; i < parameterTypes.Count; i++) { var argument = CustomAttributeArgument.FromReader(parentModule, parameterTypes[i], reader); result.FixedArguments.Add(argument); } // Read named arguments. ushort namedArgumentCount = reader.ReadUInt16(); for (int i = 0; i < namedArgumentCount; i++) { var argument = CustomAttributeNamedArgument.FromReader(parentModule, reader); result.NamedArguments.Add(argument); } return(result); }
CustomAttributeReader(ModuleDef module, IImageStream reader, ICustomAttributeType ctor) { this.module = module; this.reader = reader; this.ownReader = false; this.ctor = ctor; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; }
static MethodSig GetMethodSig(ICustomAttributeType ctor) { return(ctor == null ? null : ctor.MethodSig); }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> /// <param name="blobReader">A reader that returns the original custom attribute blob data</param> public CustomAttribute(ICustomAttributeType ctor, IEnumerable<CAArgument> arguments, IEnumerable<CANamedArgument> namedArguments, IBinaryReader blobReader) { this.ctor = ctor; this.arguments = arguments == null ? ThreadSafeListCreator.Create<CAArgument>() : ThreadSafeListCreator.Create<CAArgument>(arguments); this.namedArguments = namedArguments == null ? ThreadSafeListCreator.Create<CANamedArgument>() : ThreadSafeListCreator.Create<CANamedArgument>(namedArguments); this.blobReader = blobReader; }
static MethodSig GetMethodSig(ICustomAttributeType ctor) => ctor?.MethodSig;
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments. The list is now owned by this instance.</param> /// <param name="namedArguments">Named arguments. The list is now owned by this instance.</param> internal CustomAttribute(ICustomAttributeType ctor, List <CAArgument> arguments, List <CANamedArgument> namedArguments) { this.ctor = ctor; this.arguments = arguments == null ? new List <CAArgument>() : arguments; this.namedArguments = namedArguments == null ? new List <CANamedArgument>() : namedArguments; }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, IEnumerable <CAArgument> arguments, IEnumerable <CANamedArgument> namedArguments) { this.ctor = ctor; this.arguments = arguments == null ? new List <CAArgument>() : new List <CAArgument>(arguments); this.namedArguments = namedArguments == null ? new List <CANamedArgument>() : new List <CANamedArgument>(namedArguments); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset) { if (ctor == null) return CreateEmpty(ctor); using (var reader = new CustomAttributeReader(readerModule, offset)) { try { return reader.Read(ctor); } catch (CABlobParserException) { return new CustomAttribute(ctor, reader.GetRawBlob()); } catch (IOException) { return new CustomAttribute(ctor, reader.GetRawBlob()); } } }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> public CustomAttribute(ICustomAttributeType ctor) : this(ctor, null, null, null) { }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments. The list is now owned by this instance.</param> /// <param name="namedArguments">Named arguments. The list is now owned by this instance.</param> internal CustomAttribute(ICustomAttributeType ctor, List<CAArgument> arguments, List<CANamedArgument> namedArguments) { this.ctor = ctor; this.arguments = arguments == null ? new List<CAArgument>() : arguments; this.namedArguments = namedArguments == null ? new List<CANamedArgument>() : namedArguments; }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, ThreadSafe.IEnumerable <CANamedArgument> namedArguments) : this(ctor, null, namedArguments) { }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments. The list is now owned by this instance.</param> /// <param name="namedArguments">Named arguments. The list is now owned by this instance.</param> /// <param name="blobReader">A reader that returns the original custom attribute blob data</param> internal CustomAttribute(ICustomAttributeType ctor, List<CAArgument> arguments, List<CANamedArgument> namedArguments, IBinaryReader blobReader) { this.ctor = ctor; this.arguments = arguments == null ? ThreadSafeListCreator.Create<CAArgument>() : ThreadSafeListCreator.MakeThreadSafe(arguments); this.namedArguments = namedArguments == null ? ThreadSafeListCreator.Create<CANamedArgument>() : ThreadSafeListCreator.MakeThreadSafe(namedArguments); this.blobReader = blobReader; }
static CustomAttribute CreateEmpty(ICustomAttributeType ctor) { return(new CustomAttribute(ctor, new byte[0])); }
CustomAttributeReader(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset) { this.module = readerModule; this.reader = readerModule.BlobStream.CreateStream(offset); this.ownReader = true; this.ctor = ctor; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset, GenericParamContext gpContext) { using (var reader = new CustomAttributeReader(readerModule, offset, gpContext)) { try { if (ctor == null) return reader.CreateRaw(ctor); return reader.Read(ctor); } catch (CABlobParserException) { return reader.CreateRaw(ctor); } catch (IOException) { return reader.CreateRaw(ctor); } } }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="rawData">Raw custom attribute blob</param> public CustomAttribute(ICustomAttributeType ctor, byte[] rawData) : this(ctor, null, null, null) { this.rawData = rawData; }
CustomAttribute CreateRaw(ICustomAttributeType ctor) { return new CustomAttribute(ctor, GetRawBlob()); }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, IEnumerable <CAArgument> arguments, IEnumerable <CANamedArgument> namedArguments) : this(ctor, arguments, namedArguments, null) { }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor) { return Read(module, stream, ctor, new GenericParamContext()); }
static MethodSig GetMethodSig(ICustomAttributeType ctor) { return ctor == null ? null : ctor.MethodSig; }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="caBlob">CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDef module, byte[] caBlob, ICustomAttributeType ctor, GenericParamContext gpContext) { return Read(module, MemoryImageStream.Create(caBlob), ctor, gpContext); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset) { return(Read(readerModule, ctor, offset, new GenericParamContext())); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor, GenericParamContext gpContext) { using (var reader = new CustomAttributeReader(module, stream, gpContext)) { try { if (stream == null || ctor == null) return reader.CreateRaw(ctor); return reader.Read(ctor); } catch (CABlobParserException) { return reader.CreateRaw(ctor); } catch (IOException) { return reader.CreateRaw(ctor); } } }
CustomAttribute CreateRaw(ICustomAttributeType ctor) { return(new CustomAttribute(ctor, GetRawBlob())); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="readerModule">Reader module</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="offset">Offset of custom attribute in the #Blob stream</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDefMD readerModule, ICustomAttributeType ctor, uint offset) { return Read(readerModule, ctor, offset, new GenericParamContext()); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="caBlob">CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute"/> instance</returns> public static CustomAttribute Read(ModuleDef module, byte[] caBlob, ICustomAttributeType ctor, GenericParamContext gpContext) { return(Read(module, MemoryImageStream.Create(caBlob), ctor, gpContext)); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <returns>A new <see cref="CustomAttribute"/> instance or <c>null</c> if one of the /// args is <c>null</c> or if we failed to parse the CA blob</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor) { if (stream == null || ctor == null) return null; try { using (var reader = new CustomAttributeReader(module, stream)) return reader.Read(ctor); } catch (CABlobParserException) { return null; } catch (IOException) { return null; } }
CustomAttribute Read(ICustomAttributeType ctor) { var methodSig = ctor == null ? null : ((IMethodDefOrRef)ctor).MethodSig; if (methodSig == null) throw new CABlobParserException("ctor is null or not a method"); var mrCtor = ctor as MemberRef; if (mrCtor != null) { var owner = mrCtor.Class as TypeSpec; if (owner != null) { var gis = owner.TypeSig as GenericInstSig; if (gis != null) { genericArguments = new GenericArguments(); genericArguments.PushTypeArgs(gis.GenericArguments); } } } bool isEmpty = methodSig.Params.Count == 0 && reader.Position == reader.Length; if (!isEmpty && reader.ReadUInt16() != 1) throw new CABlobParserException("Invalid CA blob prolog"); var ctorArgs = new List<CAArgument>(methodSig.Params.Count); foreach (var arg in methodSig.Params.GetSafeEnumerable()) ctorArgs.Add(ReadFixedArg(FixTypeSig(arg))); // Some tools don't write the next ushort if there are no named arguments. int numNamedArgs = reader.Position == reader.Length ? 0 : reader.ReadUInt16(); var namedArgs = ReadNamedArguments(numNamedArgs); // verifyReadAllBytes will be set when we guess the underlying type of an enum. // To make sure we guessed right, verify that we read all bytes. if (verifyReadAllBytes && reader.Position != reader.Length) throw new CABlobParserException("Not all CA blob bytes were read"); return new CustomAttribute(ctor, ctorArgs, namedArgs); }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, IEnumerable<CAArgument> arguments, IEnumerable<CANamedArgument> namedArguments) : this(ctor, arguments, namedArguments, null) { }
static MethodSig GetMethodSig(ICustomAttributeType ctor) { var mrCtor = ctor as MemberRef; if (mrCtor != null) return mrCtor.MethodSig; var mdCtor = ctor as MethodDef; if (mdCtor != null) return mdCtor.MethodSig; return null; }
/// <summary> /// Constructor /// </summary> /// <param name="ctor">Custom attribute constructor</param> /// <param name="arguments">Constructor arguments or <c>null</c> if none</param> /// <param name="namedArguments">Named arguments or <c>null</c> if none</param> public CustomAttribute(ICustomAttributeType ctor, IEnumerable<CAArgument> arguments, IEnumerable<CANamedArgument> namedArguments) { this.ctor = ctor; this.arguments = arguments == null ? new List<CAArgument>() : new List<CAArgument>(arguments); this.namedArguments = namedArguments == null ? new List<CANamedArgument>() : new List<CANamedArgument>(namedArguments); }