• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xaviersolau / GeneratorTools / 17747266492

15 Sep 2025 09:32PM UTC coverage: 86.455% (-0.05%) from 86.508%
17747266492

Pull #71

github

web-flow
Merge ed706ddac into d90b841da
Pull Request #71: Improve metadata assembly loader.

4 of 7 new or added lines in 1 file covered. (57.14%)

3817 of 4415 relevant lines covered (86.46%)

11990.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.59
/src/libs/SoloX.GeneratorTools.Core.CSharp/Workspace/Impl/CSharpMetadataAssembly.cs
1
// ----------------------------------------------------------------------
2
// <copyright file="CSharpMetadataAssembly.cs" company="Xavier Solau">
3
// Copyright © 2021 Xavier Solau.
4
// Licensed under the MIT license.
5
// See LICENSE file in the project root for full license information.
6
// </copyright>
7
// ----------------------------------------------------------------------
8

9
using Microsoft.CodeAnalysis;
10
using SoloX.GeneratorTools.Core.CSharp.Model;
11
using SoloX.GeneratorTools.Core.CSharp.Model.Impl;
12
using SoloX.GeneratorTools.Core.Utils;
13
using System;
14
using System.Collections.Generic;
15
using System.IO;
16
using System.Reflection.Metadata;
17
using System.Reflection.PortableExecutable;
18

19
namespace SoloX.GeneratorTools.Core.CSharp.Workspace.Impl
20
{
21
    /// <summary>
22
    /// Implement ICSharpMetadataAssembly in order to load all type declarations form MetadataReader.
23
    /// </summary>
24
    public class CSharpMetadataAssembly : ICSharpMetadataAssembly, ICSharpWorkspaceItemLoader<ICSharpMetadataAssembly>
25
    {
26
        private readonly IGeneratorLogger<CSharpMetadataAssembly> logger;
27
        private readonly IMetadataDeclarationFactory declarationFactory;
28
        private bool isLoaded;
29

30
        /// <summary>
31
        /// Initializes a new instance of the <see cref="CSharpMetadataAssembly"/> class.
32
        /// </summary>
33
        /// <param name="logger">The logger to log errors.</param>
34
        /// <param name="declarationFactory">The declaration factory to use to create declaration instances.</param>
35
        /// <param name="assemblyPath">The assembly to load declaration from.</param>
36
        public CSharpMetadataAssembly(IGeneratorLogger<CSharpMetadataAssembly> logger, IMetadataDeclarationFactory declarationFactory, string assemblyPath)
37
        {
38
            this.logger = logger;
452✔
39
            this.declarationFactory = declarationFactory;
452✔
40
            AssemblyPath = assemblyPath;
41
        }
452✔
42

43
        /// <inheritdoc/>
44
        public string AssemblyPath { get; }
45

46
        /// <inheritdoc/>
47
        public IReadOnlyCollection<IDeclaration<SyntaxNode>> Declarations { get; private set; }
48

49
        /// <inheritdoc/>
50
        public ICSharpMetadataAssembly WorkspaceItem => this;
400✔
51

52
        /// <inheritdoc/>
53
        public void Load(ICSharpWorkspace workspace)
54
        {
55
            if (workspace == null)
452✔
56
            {
57
                throw new ArgumentNullException(nameof(workspace), $"The argument {nameof(workspace)} was null.");
×
58
            }
59

60
            if (this.isLoaded)
452✔
61
            {
62
                return;
×
63
            }
64

65
            this.isLoaded = true;
452✔
66

67
            var declarations = new List<IDeclaration<SyntaxNode>>();
452✔
68

69
            try
70
            {
71
                using var portableExecutableReader = new PEReader(File.OpenRead(this.AssemblyPath));
452✔
72

73
                if (!portableExecutableReader.HasMetadata)
452✔
74
                {
75
                    this.Declarations = declarations;
×
76
                    return;
×
77
                }
78

79
                var metadataReader = portableExecutableReader.GetMetadataReader();
452✔
80

81
                foreach (var assemblyReferenceHandle in metadataReader.AssemblyReferences)
7,752✔
82
                {
83
                    var assemblyReference = metadataReader.GetAssemblyReference(assemblyReferenceHandle);
3,424✔
84

85
                    var assemblyName = metadataReader.GetString(assemblyReference.Name);
3,424✔
86
                    var assemblyFile = $"{assemblyName}.dll";
3,424✔
87

88
                    try
89
                    {
90
                        workspace.RegisterMetadataAssembly(assemblyFile);
3,424✔
91
                    }
3,424✔
NEW
92
                    catch (FileNotFoundException e)
×
93
                    {
NEW
94
                        this.logger?.LogWarning(e, $"Could not load types from {assemblyFile}");
×
NEW
95
                    }
×
96
                }
97

98
                foreach (var typeDefinitionHandle in metadataReader.TypeDefinitions)
111,752✔
99
                {
100
                    var declaration = this.declarationFactory.CreateDeclaration(metadataReader, typeDefinitionHandle, AssemblyPath);
55,424✔
101

102
                    if (declaration != null)
55,424✔
103
                    {
104
                        declarations.Add(declaration);
19,904✔
105
                    }
106
                }
107
            }
452✔
108
            catch (FileNotFoundException e)
×
109
            {
110
                this.logger?.LogWarning(e, $"Could not load types from {this.AssemblyPath}");
×
111
            }
×
112

113
            this.Declarations = declarations;
452✔
114
        }
452✔
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc