Rank: Member
Groups: Registered
Joined: 8/19/2018(UTC) Posts: 10 Location: France
Thanks: 1 times Was thanked: 1 time(s) in 1 post(s)
|
Kiaora, I met a problem with the il injection when using System.Reflection.Metadata it seem's the pdb file can't be read anymore. This code is working in simple runner as in live unit testing but fail with ncrunch. Code:
using System;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Xunit;
namespace MetadataTests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
using (var stream = File.OpenRead(typeof(UnitTest1).Assembly.Location))
using (var reader = new PEReader(stream))
{
Func<string, Stream> streamProvider = p => new FileStream(p, FileMode.Open, FileAccess.Read);
var metadata = reader.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections);
var pdbLoaded = reader.TryOpenAssociatedPortablePdb(stream.Name, streamProvider, out var metadataReaderProvider,
out var pdbPath);
Assert.True(pdbLoaded);
var metadataSymbol = metadataReaderProvider.GetMetadataReader();
var type = metadata.TypeDefinitions.Select(metadata.GetTypeDefinition).First(a => metadata.GetString(a.Name).Equals(nameof(UnitTest1)));
var meta = type.GetMethods().Select(a => metadataSymbol.GetMethodDebugInformation(a)).ToList();
var debug = meta.SelectMany(a => a.GetSequencePoints()).Select(a => a.Document).First();
var docu = metadataSymbol.GetDocument(debug);
var name = metadataSymbol.GetString(docu.Name);
Assert.Contains("UnitTest1.cs", name);
}
}
}
}
|