public IDictionary <IMetadataMember, MetadataToken> UnlockMetadata() { var buffer = new MetadataBuffer(Image); buffer.TableStreamBuffer.AddAssembly(Image.Assembly); NetDirectory.ResourcesManifest = buffer.ResourcesBuffer.CreateDirectory(); var buffers = new MetadataStreamBuffer[] { buffer.TableStreamBuffer, buffer.BlobStreamBuffer, buffer.GuidStreamBuffer, buffer.StringStreamBuffer, buffer.UserStringStreamBuffer }; foreach (var streamBuffer in buffers) { var header = StreamHeaders.FirstOrDefault(x => x.Name == streamBuffer.Name) ?? new MetadataStreamHeader(streamBuffer.Name); header.Stream = streamBuffer.CreateStream(); } Image = null; return(buffer.TableStreamBuffer.GetNewTokenMapping()); }
/// <summary> /// Gets the first occuring metadata heap stream that inherits from the given type argument. /// </summary> /// <typeparam name="TStream">The type of the metadata stream.</typeparam> /// <returns></returns> public TStream GetStream <TStream>() where TStream : MetadataStream { var header = StreamHeaders.FirstOrDefault(x => x.Stream is TStream); return(header != null ? (TStream)header.Stream : null); }
public IDictionary <IMetadataMember, MetadataToken> UnlockMetadata() { if (!IsLocked) { throw new InvalidOperationException("Cannot unlock the metadata if it has not already been locked."); } var image = Image; var buffer = new MetadataBuffer(image); // Add assembly to buffer. buffer.TableStreamBuffer.AddAssembly(image.Assembly); // Create resources. NetDirectory.ResourcesManifest = buffer.ResourcesBuffer.CreateDirectory(); // Unlock metadata. Image = null; // Replace old streams with new buffers. var buffers = new MetadataStreamBuffer[] { buffer.TableStreamBuffer, buffer.BlobStreamBuffer, buffer.GuidStreamBuffer, buffer.StringStreamBuffer, buffer.UserStringStreamBuffer }; foreach (var streamBuffer in buffers) { var header = StreamHeaders.FirstOrDefault(x => x.Name == streamBuffer.Name); if (header == null) { header = new MetadataStreamHeader(streamBuffer.Name); StreamHeaders.Add(header); } header.Stream = streamBuffer.CreateStream(); } // Update managed entrypoint. var newTokenMapping = buffer.TableStreamBuffer.GetNewTokenMapping(); NetDirectory.EntryPointToken = image.ManagedEntrypoint != null ? newTokenMapping[image.ManagedEntrypoint].ToUInt32() : 0u; return(newTokenMapping); }
/// <summary> /// Gets the first occuring metadata heap stream with the given name. /// </summary> /// <param name="name">The name of the stream to get.</param> /// <returns></returns> public MetadataStream GetStream(string name) { var header = StreamHeaders.FirstOrDefault(x => x.Name == name); return(header != null ? header.Stream : null); }
public IDictionary <IMetadataMember, MetadataToken> UnlockMetadata(IMetadataBuilder builder) { if (!IsLocked) { throw new InvalidOperationException("Cannot unlock the metadata if it has not already been locked."); } var image = Image; // Construct new metadata streams. var buffer = builder.Rebuild(image); // Create resources. NetDirectory.ResourcesManifest = buffer.ResourcesBuffer.CreateDirectory(); // Serialize new streams. var newStreams = new MetadataStreamBuffer[] { buffer.TableStreamBuffer, buffer.BlobStreamBuffer, buffer.GuidStreamBuffer, buffer.StringStreamBuffer, buffer.UserStringStreamBuffer }.ToDictionary(x => x, x => x.CreateStream()); // Determine new entrypoint token. var newTokenMapping = buffer.TableStreamBuffer.GetNewTokenMapping(); uint entrypointToken; if (image.ManagedEntrypoint == null) { entrypointToken = 0u; } else { if (newTokenMapping.TryGetValue(image.ManagedEntrypoint, out var token)) { entrypointToken = token.ToUInt32(); } else { throw new MemberNotImportedException(image.ManagedEntrypoint); } } // Unlock metadata, commit changes to streams. Image = null; foreach (var entry in newStreams) { var header = StreamHeaders.FirstOrDefault(x => x.Name == entry.Key.Name); if (header == null) { header = new MetadataStreamHeader(entry.Key.Name); StreamHeaders.Add(header); } header.Stream = entry.Value; } // Update managed entrypoint. NetDirectory.EntryPointToken = entrypointToken; return(newTokenMapping); }