Determine filename of Interface declaration #878
-
My goal is to determine the filename of an interface declaration. For most classes this is trivial using the This discussion is related #831 Interfaces cannot have a method body, and thus no This is the working implementation for any typedef with a methodbody, that I am using: using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Pdb;
IEnumerable<TypeFile> GetTypesFiles(string dir, string dllFile, string pdbFile) {
DefaultAssemblyResolver asmResolver = new();
asmResolver.AddSearchDirectory(dir);
ReaderParameters parameters = new() { ReadSymbols = true};
ModuleDefinition mod = ModuleDefinition.ReadModule(dllFile, parameters);
PdbReaderProvider pdbReader = new();
ISymbolReader symbols = pdbReader.GetSymbolReader(mod, pdbFile);
foreach (TypeDefinition type in mod.GetTypes()) {
if (!type.HasMethods) {
continue;
}
foreach (string file in type.Methods
.Select(method => symbols.Read(method))
.Select(symbol => symbol.SequencePoints
.Where(static (SequencePoint seq) => seq is not null)
.Select(static (SequencePoint seq) => seq.Document.Url)
.Distinct()) // partial classes span multiple documents
.SelectMany(seqs => seqs)
.Where(seq => seq is not null)) {
yield return new(type, file);
}
}
}
string SourceAssemblyFolder = "publish";
string SourceAssemblyFileName = "publish/test.dll";
string SourceSymbolsFileName = "publish/test.pdb";
var defs = GetTypesFiles(SourceAssemblyFolder, SourceAssemblyFileName, SourceSymbolsFileName).ToArray();
foreach (var def in defs) {
Console.WriteLine(def.ToString());
}
record struct TypeFile(TypeDefinition type, string filepath); If the test assembly exclusively contains abstract classes without method bodies or interfaces, as expected, nothing is output. interface ITest {
string Name { get; set }
string Surname { get; set; }
string FullName();
}
abstract class Test : ITest {
abstract string Name { get; set }
abstract string Surname { get; set; }
abstract string FullName();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I solved it using the regular dotnet tool install --global SourceLinkExtract
dotnet tool install --global SourceSymbolMapper
extract test.pdb meta.json out
mapper meta.json out symbols.json |
Beta Was this translation helpful? Give feedback.
I solved it using the regular
MetadataReader
. In the hope that it will help someone after me, I published two tools that extract the data I require.