jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.76k stars 627 forks source link

Error during the compilation in VisualStudio's msbuild.exe #961

Open alexkr403 opened 1 month ago

alexkr403 commented 1 month ago

I try to use the library for Roslyn code generation purposes. The code inside the generator:

public Dictionary<string, string> Create()
{
    var stream = Assembly
        .GetAssembly(typeof(ParametersXmlParser))
        .GetManifestResourceStream("Compatibility_Assembly");

    var module = ModuleDefinition.ReadModule(stream);

    var tagType = module.GetType("Compatibility.Enumerations.Tag");
    var derivedFromAttributeType =
        module.GetType("Compatibility.Attributes.ParameterDerivedFromAttribute");

    var valueToNameLookup = tagType
        .Fields
        .Where(f => f.Name != "value__")
        .ToDictionary(f => (int)f.Constant, f => f.Name);

    var result = new Dictionary<string, string>();

    foreach (var field in tagType.Fields)
    {
        if (field.Name == "value__")
        {
            continue;
        }

        if (field.HasCustomAttributes)
        {
            var derivedFrom = field.Name;

            var derivedFromAttribute = field.CustomAttributes.FirstOrDefault(attribute =>
                attribute.AttributeType.Resolve() == derivedFromAttributeType);
 ....
        }
    }

    return ...

There is an error during the compilation in Visual Studio or in pwsh via the command "./MSBuild.exe --restore "D:\source\projectName.csproj"" on the line "attribute.AttributeType.Resolve()"

Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 2> at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters) in C:\sources\cecil\Mono.Cecil\BaseAssemblyResolver.cs:line 172 2> at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name) in C:\sources\cecil\Mono.Cecil\DefaultAssemblyResolver.cs:line 33 2> at Mono.Cecil.MetadataResolver.Resolve(TypeReference type) in C:\sources\cecil\Mono.Cecil\MetadataResolver.cs:line 111 2> at Mono.Cecil.TypeReference.Resolve() in C:\sources\cecil\Mono.Cecil\TypeReference.cs:line 278 2> at CodeGenerator.Parameters.Parser.ParametersXmlParser.Create() in D:\source\code-generators\sources\CodeGenerator.Parameters\Parser\ParametersXmlParser.cs:line 84

Compilation via the command "dotnet build "D:\source\projectName.csproj"" works smoothly. The most interesting is that despite the error during the compilation the generator itself works after the build of the solution is finished with error, if I click "re-run" to generate the code.