public void PersistentConstructor() { var module = ModuleDefinition.FromFile(typeof(CustomAttributesTestClass).Assembly.Location); using var stream = new MemoryStream(); module.Write(stream); module = ModuleDefinition.FromReader(new ByteArrayReader(stream.ToArray())); var type = module.TopLevelTypes.First(t => t.Name == nameof(CustomAttributesTestClass)); Assert.All(type.CustomAttributes, a => Assert.Equal(nameof(TestCaseAttribute), a.Constructor.DeclaringType.Name)); }
private static MethodDefinition LookupMethod(string methodName, bool rebuild) { var module = ModuleDefinition.FromFile(typeof(SecurityAttributes).Assembly.Location); if (rebuild) { var stream = new MemoryStream(); module.Write(stream); module = ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray())); } var type = module.TopLevelTypes.First(t => t.Name == nameof(SecurityAttributes)); return(type.Methods.First(m => m.Name == methodName)); }
public void PersistentResources() { var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld); // Add new directory. const string directoryName = "Test"; var entryData = new byte[] { 0, 1, 2, 3, 4 }; var directory = new ResourceDirectory(directoryName) { Entries = { new ResourceDirectory(1) { Entries ={ new ResourceData(1234, new DataSegment(entryData)) } } } }; module.NativeResourceDirectory.Entries.Add(directory); // Write and rebuild. using var stream = new MemoryStream(); module.Write(stream); var newModule = ModuleDefinition.FromReader(new ByteArrayReader(stream.ToArray())); // Assert contents. var newDirectory = (IResourceDirectory)newModule.NativeResourceDirectory.Entries .First(entry => entry.Name == directoryName); newDirectory = (IResourceDirectory)newDirectory.Entries[0]; var newData = (IResourceData)newDirectory.Entries[0]; var newContents = (IReadableSegment)newData.Contents; Assert.Equal(entryData, newContents.ToArray()); }
private static ModuleDefinition Rebuild(ModuleDefinition module) { using var stream = new MemoryStream(); module.Write(stream); return(ModuleDefinition.FromReader(new ByteArrayReader(stream.ToArray()))); }