Search code examples
.net.net-5csprojassembly-attributes

Setting AssemblyAttribute in .csproj file with enum value


I want to set an AssemblyAttribute with an enum parameter in the .csproj file. How do I achieve that?

I am aware that I can set it the following way within a file:

[assembly: TestDataSourceDiscovery(TestDataSourceDiscoveryOption.DuringExecution)]

However, I want to distribute this via NuGet (in build/MyPackage.props) and hence want to declare it there the following way (as most people do it with InternalsVisible):

<AssemblyAttribute Include="Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceDiscoveryAttribute">
  <_Parameter1>Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceDiscoveryOption.DuringExecution</_Parameter1>
</AssemblyAttribute>

I do get the error CS1503 stating

Argument 1: cannot convert from 'string' to 'Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceDiscoveryOption'

How can I provide the parameter value as TestDataSourceDiscoveryOption enum?


Solution

  • I finally found what I was looking for after digging though GitHub Issue for quite some time:

    <ItemGroup>
      <AssemblyAttribute Include="Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceDiscovery" >
        <_Parameter1>Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceDiscoveryOption.DuringExecution</_Parameter1>
        <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
      </AssemblyAttribute>
    </ItemGroup>
    

    For the interested, the solution to the requested feature after a long discussion: Add support for non-string assembly attributes#2281

    Corresponding PR: Allow parameter type name to be specified for WriteCodeFragment task #6285