Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 345
Ildrial Avatar asked Jun 26 '26 12:06

Ildrial


1 Answers

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

like image 108
Ildrial Avatar answered Jun 29 '26 05:06

Ildrial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!