Assembly attributes added by Mono.Cecil are not reflected in Windows explorer file properties. Why?

60 views Asked by At

I've got the following code. TLDR: It uses Mono.Cecil to load all attributes from one assembly and copies some of them to the destination assembly.

    public void Copy(string pSourceAssemblyPath, string pDestinationAssemblyPath)
    {
        using var originalAssembly = AssemblyDefinition.ReadAssembly(pSourceAssemblyPath);
        var customAttributes = originalAssembly.CustomAttributes;
        using var translationAssembly = AssemblyDefinition.ReadAssembly(pDestinationAssemblyPath, new ReaderParameters { ReadWrite = true });
        translationAssembly.Name.Version = originalAssembly.Name.Version;

        var copyableAttributeNames = new[]
        {
            typeof(AssemblyCompanyAttribute),
            typeof(AssemblyCopyrightAttribute),
            typeof(AssemblyDescriptionAttribute),
            typeof(AssemblyFileVersionAttribute),
            typeof(AssemblyInformationalVersionAttribute),
            typeof(AssemblyProductAttribute),
            typeof(AssemblyTitleAttribute),
            typeof(AssemblyVersionAttribute),
        };

        var attributesToCopy = copyableAttributeNames.Join(customAttributes, a => a.Name, a => a.AttributeType.Name,
                                                           (type, customAttribute) => new { Type = type, CustomAttribute = customAttribute });

        foreach (var attribute in attributesToCopy)
        {
            var constructor = attribute.Type.GetConstructors()[0];
            var constructorRef = translationAssembly.MainModule.ImportReference(constructor);
            var constructorParameter = constructor.GetParameters()[0];

            var parameterValue = attribute.CustomAttribute.ConstructorArguments[0].Value;
            var parameterType = translationAssembly.MainModule.ImportReference(constructorParameter.GetType());
            var browsableArg = new CustomAttributeArgument(parameterType, parameterValue);

            var customAttribute = new CustomAttribute(constructorRef);
            customAttribute.ConstructorArguments.Add(browsableArg);
            translationAssembly.CustomAttributes.Add(customAttribute);
        }

        translationAssembly.Write();
    }

And it kind of works find. When I open the destination assembly with IlSpy, I can see that the attributes are copied correctly:

enter image description here

But when I have a look at the file properties via Windows explorer, those values are not set:

enter image description here

Am I missing anything? Is there a way to achieve what I want via Mono Cecil?

0

There are 0 answers